Make `hir::PathSegment::hir_id` non-optional.

This commit is contained in:
Nicholas Nethercote 2022-08-30 16:54:42 +10:00
parent 6d850d936b
commit bb0ae3c446
16 changed files with 103 additions and 97 deletions

View File

@ -1776,13 +1776,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
binding: hir::HirId,
attrs: AttrVec,
) -> hir::Expr<'hir> {
let hir_id = self.next_id();
let res = Res::Local(binding);
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(span),
res,
segments: arena_vec![self; hir::PathSegment::from_ident(ident, res)],
segments: arena_vec![self; hir::PathSegment::from_ident(ident, hir_id, res)],
}),
));

View File

@ -246,9 +246,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
if let Some(hir_id) = path_segment.hir_id {
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
}
self.insert(path_span, path_segment.hir_id, Node::PathSegment(path_segment));
intravisit::walk_path_segment(self, path_span, path_segment);
}

View File

@ -1431,13 +1431,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
GenericParamKind::Const { .. } => None,
GenericParamKind::Type { .. } => {
let def_id = self.local_def_id(id).to_def_id();
let hir_id = self.next_id();
let res = Res::Def(DefKind::TyParam, def_id);
let ty_path = self.arena.alloc(hir::Path {
span: param_span,
res,
segments: self
.arena
.alloc_from_iter([hir::PathSegment::from_ident(ident, res)]),
.alloc_from_iter([hir::PathSegment::from_ident(ident, hir_id, res)]),
});
let ty_id = self.next_id();
let bounded_ty =

View File

@ -1260,6 +1260,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
}
TyKind::ImplicitSelf => {
let hir_id = self.lower_node_id(t.id);
let res = self.expect_full_res(t.id);
let res = self.lower_res(res);
hir::TyKind::Path(hir::QPath::Resolved(
@ -1268,6 +1269,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
res,
segments: arena_vec![self; hir::PathSegment::from_ident(
Ident::with_dummy_span(kw::SelfUpper),
hir_id,
res
)],
span: self.lower_span(t.span),
@ -2194,13 +2196,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::PredicateOrigin::ImplTrait,
);
let hir_id = self.next_id();
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
let ty = hir::TyKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(span),
res,
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), hir_id, res)],
}),
));

View File

@ -255,13 +255,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
)
}
Some(res) => {
let hir_id = self.next_id();
let res = self.lower_res(res);
hir::PatKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(ident.span),
res,
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), hir_id, res)],
}),
))
}

View File

@ -250,15 +250,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
let res = self.expect_full_res(segment.id);
let id = self.lower_node_id(segment.id);
let hir_id = self.lower_node_id(segment.id);
debug!(
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
segment.ident, segment.id, id,
segment.ident, segment.id, hir_id,
);
hir::PathSegment {
ident: self.lower_ident(segment.ident),
hir_id: Some(id),
hir_id,
res: self.lower_res(res),
infer_args,
args: if generic_args.is_empty() && generic_args.span.is_empty() {

View File

@ -935,10 +935,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
_,
) = hir_map.body(fn_body_id).value.kind
{
let opt_suggestions = path_segment
.hir_id
.map(|path_hir_id| self.infcx.tcx.typeck(path_hir_id.owner))
.and_then(|typeck| typeck.type_dependent_def_id(*hir_id))
let opt_suggestions = self
.infcx
.tcx
.typeck(path_segment.hir_id.owner)
.type_dependent_def_id(*hir_id)
.and_then(|def_id| self.infcx.tcx.impl_of_method(def_id))
.map(|def_id| self.infcx.tcx.associated_items(def_id))
.map(|assoc_items| {

View File

@ -203,7 +203,7 @@ pub struct PathSegment<'hir> {
/// The identifier portion of this path segment.
pub ident: Ident,
pub hir_id: Option<HirId>,
pub hir_id: HirId,
pub res: Res,
@ -223,12 +223,12 @@ pub struct PathSegment<'hir> {
impl<'hir> PathSegment<'hir> {
/// Converts an identifier to the corresponding segment.
pub fn from_ident(ident: Ident, res: Res) -> PathSegment<'hir> {
PathSegment { ident, hir_id: None, res, infer_args: true, args: None }
pub fn from_ident(ident: Ident, hir_id: HirId, res: Res) -> PathSegment<'hir> {
PathSegment { ident, hir_id, res, infer_args: true, args: None }
}
pub fn invalid() -> Self {
Self::from_ident(Ident::empty(), Res::Err)
Self::from_ident(Ident::empty(), HirId::INVALID, Res::Err)
}
pub fn args(&self) -> &GenericArgs<'hir> {

View File

@ -20,6 +20,9 @@ pub struct HirId {
}
impl HirId {
/// Signal local id which should never be used.
pub const INVALID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::INVALID };
#[inline]
pub fn expect_owner(self) -> LocalDefId {
assert_eq!(self.local_id.index(), 0);

View File

@ -724,7 +724,7 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(
segment: &'v PathSegment<'v>,
) {
visitor.visit_ident(segment.ident);
walk_list!(visitor, visit_id, segment.hir_id);
visitor.visit_id(segment.hir_id);
if let Some(ref args) = segment.args {
visitor.visit_generic_args(path_span, args);
}

View File

@ -909,7 +909,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
None?
}
let substs = self.node_substs_opt(expr.hir_id)?;
let span = tcx.hir().span(segment.hir_id?);
let span = tcx.hir().span(segment.hir_id);
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
InsertableGenericArgs {
insert_span,
@ -963,7 +963,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
if generics.has_impl_trait() {
return None;
}
let span = tcx.hir().span(segment.hir_id?);
let span = tcx.hir().span(segment.hir_id);
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
Some(InsertableGenericArgs {
insert_span,
@ -996,7 +996,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
if !segment.infer_args || generics.has_impl_trait() {
None?;
}
let span = tcx.hir().span(segment.hir_id?);
let span = tcx.hir().span(segment.hir_id);
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
InsertableGenericArgs { insert_span, substs, generics_def_id: def_id, def_id }
};

View File

@ -912,7 +912,10 @@ impl<'tcx> DumpVisitor<'tcx> {
_,
)
| Res::SelfTy { .. } => {
self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident, Res::Err));
self.dump_path_segment_ref(
id,
&hir::PathSegment::from_ident(ident, hir::HirId::INVALID, Res::Err),
);
}
def => {
error!("unexpected definition kind when processing collected idents: {:?}", def)

View File

@ -649,7 +649,7 @@ impl<'tcx> SaveContext<'tcx> {
}
pub fn get_path_segment_data(&self, path_seg: &hir::PathSegment<'_>) -> Option<Ref> {
self.get_path_segment_data_with_id(path_seg, path_seg.hir_id?)
self.get_path_segment_data_with_id(path_seg, path_seg.hir_id)
}
pub fn get_path_segment_data_with_id(

View File

@ -1113,7 +1113,7 @@ 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),
hir_id: binding.hir_id,
res: Res::Err,
args: Some(binding.gen_args),
infer_args: false,

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 {

View File

@ -166,25 +166,23 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
if let ExprKind::MethodCall(segment, ..) = expr.kind {
if let Some(hir_id) = segment.hir_id {
let hir = self.tcx.hir();
let body_id = hir.enclosing_body_owner(hir_id);
// FIXME: this is showing error messages for parts of the code that are not
// compiled (because of cfg)!
//
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
let typeck_results = self.tcx.typeck_body(
hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"),
let hir = self.tcx.hir();
let body_id = hir.enclosing_body_owner(segment.hir_id);
// FIXME: this is showing error messages for parts of the code that are not
// compiled (because of cfg)!
//
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
let typeck_results = self
.tcx
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
self.matches.insert(
segment.ident.span,
match hir.span_if_local(def_id) {
Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
None => LinkFromSrc::External(def_id),
},
);
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
self.matches.insert(
segment.ident.span,
match hir.span_if_local(def_id) {
Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
None => LinkFromSrc::External(def_id),
},
);
}
}
} else if self.handle_macro(expr.span) {
// We don't want to go deeper into the macro.