Auto merge of #101228 - nnethercote:simplify-hir-PathSegment, r=petrochenkov

Simplify `hir::PathSegment`

r? `@petrochenkov`
This commit is contained in:
bors 2022-09-05 13:36:54 +00:00
commit 2dc703fd6e
28 changed files with 172 additions and 194 deletions

View file

@ -1113,8 +1113,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let ident = Ident::new(assoc_item.name, binding.item_name.span);
let item_segment = hir::PathSegment {
ident,
hir_id: Some(binding.hir_id),
res: None,
hir_id: binding.hir_id,
res: Res::Err,
args: Some(binding.gen_args),
infer_args: false,
};
@ -1845,7 +1845,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
[.., hir::PathSegment {
ident,
args,
res: Some(Res::Def(DefKind::Enum, _)),
res: Res::Def(DefKind::Enum, _),
..
}, _] => (
// We need to include the `::` in `Type::Variant::<Args>`
@ -2127,24 +2127,22 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let types_and_spans: Vec<_> = segments
.clone()
.flat_map(|segment| {
segment.res.and_then(|res| {
if segment.args().args.is_empty() {
None
} else {
Some((
match res {
Res::PrimTy(ty) => format!("{} `{}`", res.descr(), ty.name()),
if segment.args().args.is_empty() {
None
} else {
Some((
match segment.res {
Res::PrimTy(ty) => format!("{} `{}`", segment.res.descr(), ty.name()),
Res::Def(_, def_id)
if let Some(name) = self.tcx().opt_item_name(def_id) => {
format!("{} `{name}`", res.descr())
format!("{} `{name}`", segment.res.descr())
}
Res::Err => "this type".to_string(),
_ => res.descr().to_string(),
_ => segment.res.descr().to_string(),
},
segment.ident.span,
))
}
})
}
})
.collect();
let this_type = match &types_and_spans[..] {

View file

@ -610,12 +610,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
match arg.kind {
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
[
PathSegment {
res: Some(Res::SelfTy { trait_: _, alias_to: impl_ref }),
..
},
] => {
[PathSegment { res: Res::SelfTy { trait_: _, alias_to: impl_ref }, .. }] => {
let impl_ty_name =
impl_ref.map(|(def_id, _)| self.tcx.def_path_str(def_id));
self.selftys.push((path.span, impl_ty_name));

View file

@ -768,7 +768,7 @@ impl<'tcx> TypeVisitor<'tcx> for GATSubstCollector<'tcx> {
fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
match ty.kind {
hir::TyKind::TraitObject([trait_ref], ..) => match trait_ref.trait_ref.path.segments {
[s] => s.res.and_then(|r| r.opt_def_id()) == Some(trait_def_id.to_def_id()),
[s] => s.res.opt_def_id() == Some(trait_def_id.to_def_id()),
_ => false,
},
_ => false,

View file

@ -1,6 +1,5 @@
use rustc_errors::{Applicability, StashKey};
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit;
use rustc_hir::intravisit::Visitor;
@ -179,15 +178,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
return None;
};
// Try to use the segment resolution if it is valid, otherwise we
// default to the path resolution.
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
let generics = match tcx.res_generics_def_id(res) {
let generics = match tcx.res_generics_def_id(segment.res) {
Some(def_id) => tcx.generics_of(def_id),
None => {
tcx.sess.delay_span_bug(
tcx.def_span(def_id),
&format!("unexpected anon const res {:?} in path: {:?}", res, path),
&format!("unexpected anon const res {:?} in path: {:?}", segment.res, path),
);
return None;
}

View file

@ -291,62 +291,60 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
// Creates lifetime name suggestions from the lifetime parameter names
fn get_lifetime_args_suggestions_from_param_names(
&self,
path_hir_id: Option<hir::HirId>,
path_hir_id: hir::HirId,
num_params_to_take: usize,
) -> String {
debug!(?path_hir_id);
if let Some(path_hir_id) = path_hir_id {
let mut ret = Vec::new();
for (id, node) in self.tcx.hir().parent_iter(path_hir_id) {
debug!(?id);
let params = if let Some(generics) = node.generics() {
generics.params
} else if let hir::Node::Ty(ty) = node
&& let hir::TyKind::BareFn(bare_fn) = ty.kind
{
bare_fn.generic_params
} else {
&[]
};
ret.extend(params.iter().filter_map(|p| {
let hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit }
= p.kind
else { return None };
let hir::ParamName::Plain(name) = p.name else { return None };
Some(name.to_string())
}));
// Suggest `'static` when in const/static item-like.
if let hir::Node::Item(hir::Item {
kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. },
..
})
| hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Const { .. },
..
})
| hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Const { .. },
..
})
| hir::Node::ForeignItem(hir::ForeignItem {
kind: hir::ForeignItemKind::Static { .. },
..
})
| hir::Node::AnonConst(..) = node
{
ret.extend(
std::iter::repeat("'static".to_owned())
.take(num_params_to_take.saturating_sub(ret.len())),
);
}
if ret.len() >= num_params_to_take {
return ret[..num_params_to_take].join(", ");
}
// We cannot refer to lifetimes defined in an outer function.
if let hir::Node::Item(_) = node {
break;
}
let mut ret = Vec::new();
for (id, node) in self.tcx.hir().parent_iter(path_hir_id) {
debug!(?id);
let params = if let Some(generics) = node.generics() {
generics.params
} else if let hir::Node::Ty(ty) = node
&& let hir::TyKind::BareFn(bare_fn) = ty.kind
{
bare_fn.generic_params
} else {
&[]
};
ret.extend(params.iter().filter_map(|p| {
let hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit }
= p.kind
else { return None };
let hir::ParamName::Plain(name) = p.name else { return None };
Some(name.to_string())
}));
// Suggest `'static` when in const/static item-like.
if let hir::Node::Item(hir::Item {
kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. },
..
})
| hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Const { .. },
..
})
| hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Const { .. },
..
})
| hir::Node::ForeignItem(hir::ForeignItem {
kind: hir::ForeignItemKind::Static { .. },
..
})
| hir::Node::AnonConst(..) = node
{
ret.extend(
std::iter::repeat("'static".to_owned())
.take(num_params_to_take.saturating_sub(ret.len())),
);
}
if ret.len() >= num_params_to_take {
return ret[..num_params_to_take].join(", ");
}
// We cannot refer to lifetimes defined in an outer function.
if let hir::Node::Item(_) = node {
break;
}
}
@ -690,8 +688,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
num = num_trait_generics_except_self,
);
if let Some(hir_id) = self.path_segment.hir_id
&& let Some(parent_node) = self.tcx.hir().find_parent_node(hir_id)
if let Some(parent_node) = self.tcx.hir().find_parent_node(self.path_segment.hir_id)
&& let Some(parent_node) = self.tcx.hir().find(parent_node)
&& let hir::Node::Expr(expr) = parent_node {
match expr.kind {