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

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

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
@@ -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

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
@@ -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

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
@@ -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

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
@@ -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

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)