Skip to content

Commit 6d850d9

Browse files
committed
Make hir::PathSegment::res non-optional.
1 parent ee244bf commit 6d850d9

File tree

20 files changed

+65
-76
lines changed

20 files changed

+65
-76
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,12 +1776,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
17761776
binding: hir::HirId,
17771777
attrs: AttrVec,
17781778
) -> hir::Expr<'hir> {
1779+
let res = Res::Local(binding);
17791780
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
17801781
None,
17811782
self.arena.alloc(hir::Path {
17821783
span: self.lower_span(span),
1783-
res: Res::Local(binding),
1784-
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
1784+
res,
1785+
segments: arena_vec![self; hir::PathSegment::from_ident(ident, res)],
17851786
}),
17861787
));
17871788

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 2 deletions
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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12671267
self.arena.alloc(hir::Path {
12681268
res,
12691269
segments: arena_vec![self; hir::PathSegment::from_ident(
1270-
Ident::with_dummy_span(kw::SelfUpper)
1270+
Ident::with_dummy_span(kw::SelfUpper),
1271+
res
12711272
)],
12721273
span: self.lower_span(t.span),
12731274
}),
@@ -2193,12 +2194,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21932194
hir::PredicateOrigin::ImplTrait,
21942195
);
21952196

2197+
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
21962198
let ty = hir::TyKind::Path(hir::QPath::Resolved(
21972199
None,
21982200
self.arena.alloc(hir::Path {
21992201
span: self.lower_span(span),
2200-
res: Res::Def(DefKind::TyParam, def_id.to_def_id()),
2201-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
2202+
res,
2203+
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
22022204
}),
22032205
));
22042206

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 11 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
259259
hir::PathSegment {
260260
ident: self.lower_ident(segment.ident),
261261
hir_id: Some(id),
262-
res: Some(self.lower_res(res)),
262+
res: self.lower_res(res),
263263
infer_args,
264264
args: if generic_args.is_empty() && generic_args.span.is_empty() {
265265
None

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
957957
path.segments
958958
.iter()
959959
.filter_map(move |segment| {
960-
let res = segment.res?;
960+
let res = segment.res;
961961
let generics_def_id = tcx.res_generics_def_id(res)?;
962962
let generics = tcx.generics_of(generics_def_id);
963963
if generics.has_impl_trait() {

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

Lines changed: 5 additions & 10 deletions
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

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

compiler/rustc_passes/src/stability.rs

Lines changed: 1 addition & 1 deletion
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,

0 commit comments

Comments
 (0)