Make hir::PathSegment::res non-optional.

This commit is contained in:
Nicholas Nethercote 2022-08-30 15:10:28 +10:00
parent ee244bf196
commit 6d850d936b
20 changed files with 65 additions and 76 deletions

View File

@ -1776,12 +1776,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
binding: hir::HirId,
attrs: AttrVec,
) -> hir::Expr<'hir> {
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: Res::Local(binding),
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
res,
segments: arena_vec![self; hir::PathSegment::from_ident(ident, res)],
}),
));

View File

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

View File

@ -1267,7 +1267,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.arena.alloc(hir::Path {
res,
segments: arena_vec![self; hir::PathSegment::from_ident(
Ident::with_dummy_span(kw::SelfUpper)
Ident::with_dummy_span(kw::SelfUpper),
res
)],
span: self.lower_span(t.span),
}),
@ -2193,12 +2194,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::PredicateOrigin::ImplTrait,
);
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: Res::Def(DefKind::TyParam, def_id.to_def_id()),
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), res)],
}),
));

View File

@ -254,14 +254,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lower_sub(self),
)
}
Some(res) => hir::PatKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(ident.span),
res: self.lower_res(res),
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
}),
)),
Some(res) => {
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)],
}),
))
}
}
}

View File

@ -259,7 +259,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::PathSegment {
ident: self.lower_ident(segment.ident),
hir_id: Some(id),
res: Some(self.lower_res(res)),
res: self.lower_res(res),
infer_args,
args: if generic_args.is_empty() && generic_args.span.is_empty() {
None

View File

@ -202,13 +202,10 @@ impl Path<'_> {
pub struct PathSegment<'hir> {
/// The identifier portion of this path segment.
pub ident: Ident,
// `id` and `res` are optional. We currently only use these in save-analysis,
// any path segments without these will not have save-analysis info and
// therefore will not have 'jump to def' in IDEs, but otherwise will not be
// affected. (In general, we don't bother to get the defs for synthesized
// segments, only for segments which have come from the AST).
pub hir_id: Option<HirId>,
pub res: Option<Res>,
pub res: Res,
/// Type/lifetime parameters attached to this path. They come in
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@ -226,12 +223,12 @@ pub struct PathSegment<'hir> {
impl<'hir> PathSegment<'hir> {
/// Converts an identifier to the corresponding segment.
pub fn from_ident(ident: Ident) -> PathSegment<'hir> {
PathSegment { ident, hir_id: None, res: None, infer_args: true, args: None }
pub fn from_ident(ident: Ident, res: Res) -> PathSegment<'hir> {
PathSegment { ident, hir_id: None, res, infer_args: true, args: None }
}
pub fn invalid() -> Self {
Self::from_ident(Ident::empty())
Self::from_ident(Ident::empty(), Res::Err)
}
pub fn args(&self) -> &GenericArgs<'hir> {

View File

@ -957,7 +957,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
path.segments
.iter()
.filter_map(move |segment| {
let res = segment.res?;
let res = segment.res;
let generics_def_id = tcx.res_generics_def_id(res)?;
let generics = tcx.generics_of(generics_def_id);
if generics.has_impl_trait() {

View File

@ -154,16 +154,11 @@ impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
}
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
[segment]
if segment
.res
.map(|res| {
matches!(
res,
Res::SelfTy { trait_: _, alias_to: _ }
| Res::Def(hir::def::DefKind::TyParam, _)
)
})
.unwrap_or(false) =>
if matches!(
segment.res,
Res::SelfTy { trait_: _, alias_to: _ }
| Res::Def(hir::def::DefKind::TyParam, _)
) =>
{
self.types.push(path.span);
}

View File

@ -118,8 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
_: rustc_hir::HirId,
) {
if let Some(segment) = path.segments.iter().nth_back(1)
&& let Some(res) = &segment.res
&& lint_ty_kind_usage(cx, res)
&& lint_ty_kind_usage(cx, &segment.res)
{
let span = path.span.with_hi(
segment.args.map_or(segment.ident.span, |a| a.span_ext).hi()

View File

@ -832,7 +832,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
// added, such as `core::intrinsics::transmute`
let parents = path.segments.iter().rev().skip(1);
for path_segment in parents {
if let Some(def_id) = path_segment.res.as_ref().and_then(Res::opt_def_id) {
if let Some(def_id) = path_segment.res.opt_def_id() {
// use `None` for id to prevent deprecation check
self.tcx.check_stability_allow_unstable(
def_id,

View File

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

View File

@ -596,13 +596,14 @@ impl<'tcx> SaveContext<'tcx> {
Node::TraitRef(tr) => tr.path.res,
Node::Item(&hir::Item { kind: hir::ItemKind::Use(path, _), .. }) => path.res,
Node::PathSegment(seg) => match seg.res {
Some(res) if res != Res::Err => res,
_ => {
Node::PathSegment(seg) => {
if seg.res != Res::Err {
seg.res
} else {
let parent_node = self.tcx.hir().get_parent_node(hir_id);
self.get_path_res(parent_node)
}
},
}
Node::Expr(&hir::Expr { kind: hir::ExprKind::Struct(ref qpath, ..), .. }) => {
self.typeck_results().qpath_res(qpath, hir_id)

View File

@ -2210,12 +2210,12 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
&& let [
..,
trait_path_segment @ hir::PathSegment {
res: Some(rustc_hir::def::Res::Def(rustc_hir::def::DefKind::Trait, trait_id)),
res: rustc_hir::def::Res::Def(rustc_hir::def::DefKind::Trait, trait_id),
..
},
hir::PathSegment {
ident: assoc_item_name,
res: Some(rustc_hir::def::Res::Def(_, item_id)),
res: rustc_hir::def::Res::Def(_, item_id),
..
}
] = path.segments

View File

@ -1114,7 +1114,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let item_segment = hir::PathSegment {
ident,
hir_id: Some(binding.hir_id),
res: None,
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

@ -185,7 +185,7 @@ fn in_impl<'tcx>(
if let ItemKind::Impl(item) = &item.kind;
if let Some(of_trait) = &item.of_trait;
if let Some(seg) = of_trait.path.segments.last();
if let Some(Res::Def(_, trait_id)) = seg.res;
if let Res::Def(_, trait_id) = seg.res;
if trait_id == bin_op;
if let Some(generic_args) = seg.args;
if let Some(GenericArg::Type(other_ty)) = generic_args.args.last();

View File

@ -43,8 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
if mut_ty.mutbl == Mutability::Not;
if let TyKind::Path(ref qpath) = &mut_ty.ty.kind;
let last = last_path_segment(qpath);
if let Some(res) = last.res;
if let Some(def_id) = res.opt_def_id();
if let Some(def_id) = last.res.opt_def_id();
if cx.tcx.is_diagnostic_item(sym::Option, def_id);
if let Some(params) = last_path_segment(qpath).args ;

View File

@ -128,7 +128,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
if !bound_predicate.span.from_expansion();
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
if let Some(PathSegment {
res: Some(Res::SelfTy{ trait_: Some(def_id), alias_to: _ }), ..
res: Res::SelfTy{ trait_: Some(def_id), alias_to: _ }, ..
}) = segments.first();
if let Some(
Node::Item(