Skip to content

Commit 1283a5a

Browse files
committed
Extend HIR to track the source of a type
1 parent 262fe2a commit 1283a5a

File tree

7 files changed

+62
-17
lines changed

7 files changed

+62
-17
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use hir::{BodyId, HirId};
4444
use rustc_abi::ExternAbi;
4545
use rustc_ast::*;
4646
use rustc_errors::ErrorGuaranteed;
47+
use rustc_hir::TySource;
4748
use rustc_hir::def_id::DefId;
4849
use rustc_middle::span_bug;
4950
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
@@ -165,12 +166,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
165166
hir_id: self.next_id(),
166167
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Input(arg)),
167168
span,
169+
source: TySource::Other,
168170
}));
169171

170172
let output = self.arena.alloc(hir::Ty {
171173
hir_id: self.next_id(),
172174
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Output),
173175
span,
176+
source: TySource::Other,
174177
});
175178

176179
self.arena.alloc(hir::FnDecl {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use rustc_ast::ptr::P as AstP;
66
use rustc_ast::*;
77
use rustc_ast_pretty::pprust::expr_to_string;
88
use rustc_data_structures::stack::ensure_sufficient_stack;
9-
use rustc_hir as hir;
10-
use rustc_hir::HirId;
119
use rustc_hir::def::{DefKind, Res};
10+
use rustc_hir::{self as hir, HirId, TySource};
1211
use rustc_middle::span_bug;
1312
use rustc_middle::ty::TyCtxt;
1413
use rustc_session::errors::report_lit_error;
@@ -753,6 +752,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
753752
hir_id: self.next_id(),
754753
kind: hir::TyKind::Path(resume_ty),
755754
span: unstable_span,
755+
source: TySource::Other,
756756
};
757757
let inputs = arena_vec![self; input_ty];
758758

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5656
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5757
use rustc_hir::{
5858
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, ParamName, TraitCandidate,
59+
TySource,
5960
};
6061
use rustc_index::{Idx, IndexSlice, IndexVec};
6162
use rustc_macros::extension;
@@ -1165,7 +1166,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11651166
bounds,
11661167
TaggedRef::new(lifetime_bound, TraitObjectSyntax::None),
11671168
);
1168-
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1169+
return hir::Ty {
1170+
kind,
1171+
span: self.lower_span(t.span),
1172+
hir_id: self.next_id(),
1173+
source: TySource::Other,
1174+
};
11691175
}
11701176

11711177
let id = self.lower_node_id(t.id);
@@ -1182,7 +1188,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11821188
}
11831189

11841190
fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1185-
hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
1191+
hir::Ty {
1192+
hir_id: self.next_id(),
1193+
kind,
1194+
span: self.lower_span(span),
1195+
source: TySource::Other,
1196+
}
11861197
}
11871198

11881199
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
@@ -1227,7 +1238,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12271238
let lifetime = self.lower_lifetime(&region);
12281239
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));
12291240
let span = self.lower_span(t.span);
1230-
let arg = hir::Ty { kind, span, hir_id: self.next_id() };
1241+
let arg = hir::Ty { kind, span, hir_id: self.next_id(), source: TySource::Other };
12311242
let args = self.arena.alloc(hir::GenericArgs {
12321243
args: self.arena.alloc([hir::GenericArg::Type(self.arena.alloc(arg))]),
12331244
constraints: &[],
@@ -1388,7 +1399,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13881399
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
13891400
};
13901401

1391-
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1402+
hir::Ty {
1403+
kind,
1404+
span: self.lower_span(t.span),
1405+
hir_id: self.lower_node_id(t.id),
1406+
source: TySource::Other,
1407+
}
13921408
}
13931409

13941410
/// Lowers a `ReturnPositionOpaqueTy` (`-> impl Trait`) or a `TypeAliasesOpaqueTy` (`type F =
@@ -2354,7 +2370,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23542370
}
23552371
}
23562372

2357-
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2373+
fn ty_path(&mut self, hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2374+
self.ty_path_with_source(hir_id, span, qpath, TySource::Other)
2375+
}
2376+
2377+
fn ty_path_with_source(
2378+
&mut self,
2379+
mut hir_id: HirId,
2380+
span: Span,
2381+
qpath: hir::QPath<'hir>,
2382+
source: TySource,
2383+
) -> hir::Ty<'hir> {
23582384
let kind = match qpath {
23592385
hir::QPath::Resolved(None, path) => {
23602386
// Turn trait object paths into `TyKind::TraitObject` instead.
@@ -2381,7 +2407,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23812407
_ => hir::TyKind::Path(qpath),
23822408
};
23832409

2384-
hir::Ty { hir_id, kind, span: self.lower_span(span) }
2410+
hir::Ty { hir_id, kind, span: self.lower_span(span), source }
23852411
}
23862412

23872413
/// Invoked to create the lifetime argument(s) for an elided trait object

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::sync::Arc;
22

33
use rustc_ast::{self as ast, *};
4-
use rustc_hir as hir;
5-
use rustc_hir::GenericArg;
64
use rustc_hir::def::{DefKind, PartialRes, Res};
75
use rustc_hir::def_id::DefId;
6+
use rustc_hir::{self as hir, GenericArg, TySource};
87
use rustc_middle::span_bug;
98
use rustc_session::parse::add_feature_diagnostics;
109
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
@@ -170,7 +169,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
170169
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
171170
// `<I as Iterator>::Item::default`.
172171
let new_id = self.next_id();
173-
self.arena.alloc(self.ty_path(new_id, path.span, hir::QPath::Resolved(qself, path)))
172+
self.arena.alloc(self.ty_path_with_source(
173+
new_id,
174+
path.span,
175+
hir::QPath::Resolved(qself, path),
176+
TySource::ImplicitSelf,
177+
))
174178
};
175179

176180
// Anything after the base path are associated "extensions",

compiler/rustc_hir/src/hir.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,12 @@ pub struct InferArg {
373373

374374
impl InferArg {
375375
pub fn to_ty(&self) -> Ty<'static> {
376-
Ty { kind: TyKind::Infer(()), span: self.span, hir_id: self.hir_id }
376+
Ty {
377+
kind: TyKind::Infer(()),
378+
span: self.span,
379+
hir_id: self.hir_id,
380+
source: TySource::Other,
381+
}
377382
}
378383
}
379384

@@ -3120,6 +3125,12 @@ impl<'hir> AssocItemConstraintKind<'hir> {
31203125
}
31213126
}
31223127

3128+
#[derive(Debug, Clone, Copy, PartialEq, HashStable_Generic)]
3129+
pub enum TySource {
3130+
ImplicitSelf,
3131+
Other,
3132+
}
3133+
31233134
/// An uninhabited enum used to make `Infer` variants on [`Ty`] and [`ConstArg`] be
31243135
/// unreachable. Zero-Variant enums are guaranteed to have the same layout as the never
31253136
/// type.
@@ -3139,6 +3150,7 @@ pub struct Ty<'hir, Unambig = ()> {
31393150
pub hir_id: HirId,
31403151
pub span: Span,
31413152
pub kind: TyKind<'hir, Unambig>,
3153+
pub source: TySource,
31423154
}
31433155

31443156
impl<'hir> Ty<'hir, AmbigArg> {
@@ -4837,7 +4849,7 @@ mod size_asserts {
48374849
static_assert_size!(StmtKind<'_>, 16);
48384850
static_assert_size!(TraitItem<'_>, 88);
48394851
static_assert_size!(TraitItemKind<'_>, 48);
4840-
static_assert_size!(Ty<'_>, 48);
4852+
static_assert_size!(Ty<'_>, 56);
48414853
static_assert_size!(TyKind<'_>, 32);
48424854
// tidy-alphabetical-end
48434855
}

compiler/rustc_hir/src/hir/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ macro_rules! define_tests {
2020

2121
define_tests! {
2222
cast_never TyKind Never {}
23-
cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }] }
24-
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }, mutbl: Mutability::Not }}
23+
cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never, source: TySource::Other }] }
24+
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never, source: TySource::Other }, mutbl: Mutability::Not }}
2525
cast_array TyKind Array {
26-
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never },
26+
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never, source: TySource::Other },
2727
1: &ConstArg { hir_id: HirId::INVALID, kind: ConstArgKind::Anon(&AnonConst {
2828
hir_id: HirId::INVALID,
2929
def_id: LocalDefId { local_def_index: DefIndex::ZERO },

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ fn first_non_private<'tcx>(
16771677
}
16781678

16791679
fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type {
1680-
let hir::Ty { hir_id, span, ref kind } = *hir_ty;
1680+
let hir::Ty { hir_id, span, ref kind, source: _ } = *hir_ty;
16811681
let hir::TyKind::Path(qpath) = kind else { unreachable!() };
16821682

16831683
match qpath {

0 commit comments

Comments
 (0)