Skip to content

Commit 28aaf61

Browse files
committed
Make hir::PathSegment::res non-optional.
1 parent 2aac656 commit 28aaf61

File tree

20 files changed

+65
-76
lines changed

20 files changed

+65
-76
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1770,12 +1770,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
17701770
binding: hir::HirId,
17711771
attrs: AttrVec,
17721772
) -> hir::Expr<'hir> {
1773+
let res = Res::Local(binding);
17731774
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
17741775
None,
17751776
self.arena.alloc(hir::Path {
17761777
span: self.lower_span(span),
1777-
res: Res::Local(binding),
1778-
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
1778+
res,
1779+
segments: arena_vec![self; hir::PathSegment::from_ident(ident, res)],
17791780
}),
17801781
));
17811782

compiler/rustc_ast_lowering/src/item.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1431,10 +1431,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14311431
GenericParamKind::Const { .. } => None,
14321432
GenericParamKind::Type { .. } => {
14331433
let def_id = self.local_def_id(id).to_def_id();
1434+
let res = Res::Def(DefKind::TyParam, def_id);
14341435
let ty_path = self.arena.alloc(hir::Path {
14351436
span: param_span,
1436-
res: Res::Def(DefKind::TyParam, def_id),
1437-
segments: self.arena.alloc_from_iter([hir::PathSegment::from_ident(ident)]),
1437+
res,
1438+
segments: self
1439+
.arena
1440+
.alloc_from_iter([hir::PathSegment::from_ident(ident, res)]),
14381441
});
14391442
let ty_id = self.next_id();
14401443
let bounded_ty =

compiler/rustc_ast_lowering/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12641264
self.arena.alloc(hir::Path {
12651265
res,
12661266
segments: arena_vec![self; hir::PathSegment::from_ident(
1267-
Ident::with_dummy_span(kw::SelfUpper)
1267+
Ident::with_dummy_span(kw::SelfUpper),
1268+
res
12681269
)],
12691270
span: self.lower_span(t.span),
12701271
}),
@@ -2190,12 +2191,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21902191
hir::PredicateOrigin::ImplTrait,
21912192
);
21922193

2194+
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
21932195
let ty = hir::TyKind::Path(hir::QPath::Resolved(
21942196
None,
21952197
self.arena.alloc(hir::Path {
21962198
span: self.lower_span(span),
2197-
res: Res::Def(DefKind::TyParam, def_id.to_def_id()),
2198-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
2199+
res,
2200+
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
21992201
}),
22002202
));
22012203

compiler/rustc_ast_lowering/src/pat.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
254254
lower_sub(self),
255255
)
256256
}
257-
Some(res) => hir::PatKind::Path(hir::QPath::Resolved(
258-
None,
259-
self.arena.alloc(hir::Path {
260-
span: self.lower_span(ident.span),
261-
res: self.lower_res(res),
262-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
263-
}),
264-
)),
257+
Some(res) => {
258+
let res = self.lower_res(res);
259+
hir::PatKind::Path(hir::QPath::Resolved(
260+
None,
261+
self.arena.alloc(hir::Path {
262+
span: self.lower_span(ident.span),
263+
res,
264+
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
265+
}),
266+
))
267+
}
265268
}
266269
}
267270

compiler/rustc_ast_lowering/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
260260
hir::PathSegment {
261261
ident: self.lower_ident(segment.ident),
262262
hir_id: Some(id),
263-
res: Some(self.lower_res(res)),
263+
res: self.lower_res(res),
264264
infer_args,
265265
args: if generic_args.is_empty() && generic_args.span.is_empty() {
266266
None

compiler/rustc_hir/src/hir.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,10 @@ impl Path<'_> {
202202
pub struct PathSegment<'hir> {
203203
/// The identifier portion of this path segment.
204204
pub ident: Ident,
205-
// `id` and `res` are optional. We currently only use these in save-analysis,
206-
// any path segments without these will not have save-analysis info and
207-
// therefore will not have 'jump to def' in IDEs, but otherwise will not be
208-
// affected. (In general, we don't bother to get the defs for synthesized
209-
// segments, only for segments which have come from the AST).
205+
210206
pub hir_id: Option<HirId>,
211-
pub res: Option<Res>,
207+
208+
pub res: Res,
212209

213210
/// Type/lifetime parameters attached to this path. They come in
214211
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@@ -226,12 +223,12 @@ pub struct PathSegment<'hir> {
226223

227224
impl<'hir> PathSegment<'hir> {
228225
/// Converts an identifier to the corresponding segment.
229-
pub fn from_ident(ident: Ident) -> PathSegment<'hir> {
230-
PathSegment { ident, hir_id: None, res: None, infer_args: true, args: None }
226+
pub fn from_ident(ident: Ident, res: Res) -> PathSegment<'hir> {
227+
PathSegment { ident, hir_id: None, res, infer_args: true, args: None }
231228
}
232229

233230
pub fn invalid() -> Self {
234-
Self::from_ident(Ident::empty())
231+
Self::from_ident(Ident::empty(), Res::Err)
235232
}
236233

237234
pub fn args(&self) -> &GenericArgs<'hir> {

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
886886
path.segments
887887
.iter()
888888
.filter_map(move |segment| {
889-
let res = segment.res?;
889+
let res = segment.res;
890890
let generics_def_id = tcx.res_generics_def_id(res)?;
891891
let generics = tcx.generics_of(generics_def_id);
892892
if generics.has_impl_trait() {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,11 @@ impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
154154
}
155155
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
156156
[segment]
157-
if segment
158-
.res
159-
.map(|res| {
160-
matches!(
161-
res,
162-
Res::SelfTy { trait_: _, alias_to: _ }
163-
| Res::Def(hir::def::DefKind::TyParam, _)
164-
)
165-
})
166-
.unwrap_or(false) =>
157+
if matches!(
158+
segment.res,
159+
Res::SelfTy { trait_: _, alias_to: _ }
160+
| Res::Def(hir::def::DefKind::TyParam, _)
161+
) =>
167162
{
168163
self.types.push(path.span);
169164
}

compiler/rustc_lint/src/internal.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
119119
_: rustc_hir::HirId,
120120
) {
121121
if let Some(segment) = path.segments.iter().nth_back(1)
122-
&& let Some(res) = &segment.res
123-
&& lint_ty_kind_usage(cx, res)
122+
&& lint_ty_kind_usage(cx, &segment.res)
124123
{
125124
let span = path.span.with_hi(
126125
segment.args.map_or(segment.ident.span, |a| a.span_ext).hi()

compiler/rustc_passes/src/stability.rs

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

compiler/rustc_save_analysis/src/dump_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ impl<'tcx> DumpVisitor<'tcx> {
914914
_,
915915
)
916916
| Res::SelfTy { .. } => {
917-
self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident));
917+
self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident, Res::Err));
918918
}
919919
def => {
920920
error!("unexpected definition kind when processing collected idents: {:?}", def)

compiler/rustc_save_analysis/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,14 @@ impl<'tcx> SaveContext<'tcx> {
595595
Node::TraitRef(tr) => tr.path.res,
596596

597597
Node::Item(&hir::Item { kind: hir::ItemKind::Use(path, _), .. }) => path.res,
598-
Node::PathSegment(seg) => match seg.res {
599-
Some(res) if res != Res::Err => res,
600-
_ => {
598+
Node::PathSegment(seg) => {
599+
if seg.res != Res::Err {
600+
seg.res
601+
} else {
601602
let parent_node = self.tcx.hir().get_parent_node(hir_id);
602603
self.get_path_res(parent_node)
603604
}
604-
},
605+
}
605606

606607
Node::Expr(&hir::Expr { kind: hir::ExprKind::Struct(ref qpath, ..), .. }) => {
607608
self.typeck_results().qpath_res(qpath, hir_id)

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2176,12 +2176,12 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21762176
&& let [
21772177
..,
21782178
trait_path_segment @ hir::PathSegment {
2179-
res: Some(rustc_hir::def::Res::Def(rustc_hir::def::DefKind::Trait, trait_id)),
2179+
res: rustc_hir::def::Res::Def(rustc_hir::def::DefKind::Trait, trait_id),
21802180
..
21812181
},
21822182
hir::PathSegment {
21832183
ident: assoc_item_name,
2184-
res: Some(rustc_hir::def::Res::Def(_, item_id)),
2184+
res: rustc_hir::def::Res::Def(_, item_id),
21852185
..
21862186
}
21872187
] = path.segments

compiler/rustc_typeck/src/astconv/mod.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11231123
let item_segment = hir::PathSegment {
11241124
ident,
11251125
hir_id: Some(binding.hir_id),
1126-
res: None,
1126+
res: Res::Err,
11271127
args: Some(binding.gen_args),
11281128
infer_args: false,
11291129
};
@@ -1854,7 +1854,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
18541854
[.., hir::PathSegment {
18551855
ident,
18561856
args,
1857-
res: Some(Res::Def(DefKind::Enum, _)),
1857+
res: Res::Def(DefKind::Enum, _),
18581858
..
18591859
}, _] => (
18601860
// We need to include the `::` in `Type::Variant::<Args>`
@@ -2136,24 +2136,22 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21362136
let types_and_spans: Vec<_> = segments
21372137
.clone()
21382138
.flat_map(|segment| {
2139-
segment.res.and_then(|res| {
2140-
if segment.args().args.is_empty() {
2141-
None
2142-
} else {
2143-
Some((
2144-
match res {
2145-
Res::PrimTy(ty) => format!("{} `{}`", res.descr(), ty.name()),
2139+
if segment.args().args.is_empty() {
2140+
None
2141+
} else {
2142+
Some((
2143+
match segment.res {
2144+
Res::PrimTy(ty) => format!("{} `{}`", segment.res.descr(), ty.name()),
21462145
Res::Def(_, def_id)
21472146
if let Some(name) = self.tcx().opt_item_name(def_id) => {
2148-
format!("{} `{name}`", res.descr())
2147+
format!("{} `{name}`", segment.res.descr())
21492148
}
21502149
Res::Err => "this type".to_string(),
2151-
_ => res.descr().to_string(),
2150+
_ => segment.res.descr().to_string(),
21522151
},
21532152
segment.ident.span,
21542153
))
2155-
}
2156-
})
2154+
}
21572155
})
21582156
.collect();
21592157
let this_type = match &types_and_spans[..] {

compiler/rustc_typeck/src/check/check.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -610,12 +610,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
610610
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
611611
match arg.kind {
612612
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
613-
[
614-
PathSegment {
615-
res: Some(Res::SelfTy { trait_: _, alias_to: impl_ref }),
616-
..
617-
},
618-
] => {
613+
[PathSegment { res: Res::SelfTy { trait_: _, alias_to: impl_ref }, .. }] => {
619614
let impl_ty_name =
620615
impl_ref.map(|(def_id, _)| self.tcx.def_path_str(def_id));
621616
self.selftys.push((path.span, impl_ty_name));

compiler/rustc_typeck/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl<'tcx> TypeVisitor<'tcx> for GATSubstCollector<'tcx> {
768768
fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
769769
match ty.kind {
770770
hir::TyKind::TraitObject([trait_ref], ..) => match trait_ref.trait_ref.path.segments {
771-
[s] => s.res.and_then(|r| r.opt_def_id()) == Some(trait_def_id.to_def_id()),
771+
[s] => s.res.opt_def_id() == Some(trait_def_id.to_def_id()),
772772
_ => false,
773773
},
774774
_ => false,

compiler/rustc_typeck/src/collect/type_of.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_errors::{Applicability, StashKey};
22
use rustc_hir as hir;
3-
use rustc_hir::def::Res;
43
use rustc_hir::def_id::{DefId, LocalDefId};
54
use rustc_hir::intravisit;
65
use rustc_hir::intravisit::Visitor;
@@ -180,15 +179,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
180179
return None;
181180
};
182181

183-
// Try to use the segment resolution if it is valid, otherwise we
184-
// default to the path resolution.
185-
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
186-
let generics = match tcx.res_generics_def_id(res) {
182+
let generics = match tcx.res_generics_def_id(segment.res) {
187183
Some(def_id) => tcx.generics_of(def_id),
188184
None => {
189185
tcx.sess.delay_span_bug(
190186
tcx.def_span(def_id),
191-
&format!("unexpected anon const res {:?} in path: {:?}", res, path),
187+
&format!("unexpected anon const res {:?} in path: {:?}", segment.res, path),
192188
);
193189
return None;
194190
}

src/tools/clippy/clippy_lints/src/operators/op_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn in_impl<'tcx>(
185185
if let ItemKind::Impl(item) = &item.kind;
186186
if let Some(of_trait) = &item.of_trait;
187187
if let Some(seg) = of_trait.path.segments.last();
188-
if let Some(Res::Def(_, trait_id)) = seg.res;
188+
if let Res::Def(_, trait_id) = seg.res;
189189
if trait_id == bin_op;
190190
if let Some(generic_args) = seg.args;
191191
if let Some(GenericArg::Type(other_ty)) = generic_args.args.last();

src/tools/clippy/clippy_lints/src/ref_option_ref.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
4343
if mut_ty.mutbl == Mutability::Not;
4444
if let TyKind::Path(ref qpath) = &mut_ty.ty.kind;
4545
let last = last_path_segment(qpath);
46-
if let Some(res) = last.res;
47-
if let Some(def_id) = res.opt_def_id();
46+
if let Some(def_id) = last.res.opt_def_id();
4847

4948
if cx.tcx.is_diagnostic_item(sym::Option, def_id);
5049
if let Some(params) = last_path_segment(qpath).args ;

src/tools/clippy/clippy_lints/src/trait_bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
128128
if !bound_predicate.span.from_expansion();
129129
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
130130
if let Some(PathSegment {
131-
res: Some(Res::SelfTy{ trait_: Some(def_id), alias_to: _ }), ..
131+
res: Res::SelfTy{ trait_: Some(def_id), alias_to: _ }, ..
132132
}) = segments.first();
133133
if let Some(
134134
Node::Item(

0 commit comments

Comments
 (0)