Skip to content

Commit 90265f0

Browse files
committed
Extend HIR to track the source of a type
1 parent 92331be commit 90265f0

File tree

8 files changed

+79
-30
lines changed

8 files changed

+79
-30
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

+3
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};
@@ -174,12 +175,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
174175
hir_id: self.next_id(),
175176
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Input(arg)),
176177
span,
178+
source: TySource::Other,
177179
}));
178180

179181
let output = self.arena.alloc(hir::Ty {
180182
hir_id: self.next_id(),
181183
kind: hir::TyKind::InferDelegation(sig_id, hir::InferDelegationKind::Output),
182184
span,
185+
source: TySource::Other,
183186
});
184187

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

compiler/rustc_ast_lowering/src/expr.rs

+2-2
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;
@@ -763,6 +762,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
763762
hir_id: self.next_id(),
764763
kind: hir::TyKind::Path(resume_ty),
765764
span: unstable_span,
765+
source: TySource::Other,
766766
};
767767
let inputs = arena_vec![self; input_ty];
768768

compiler/rustc_ast_lowering/src/lib.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5555
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5656
use rustc_hir::{
5757
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, LifetimeSource,
58-
LifetimeSyntax, ParamName, TraitCandidate,
58+
LifetimeSyntax, ParamName, TraitCandidate, TySource,
5959
};
6060
use rustc_index::{Idx, IndexSlice, IndexVec};
6161
use rustc_macros::extension;
@@ -1171,7 +1171,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11711171
bounds,
11721172
TaggedRef::new(lifetime_bound, TraitObjectSyntax::None),
11731173
);
1174-
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1174+
return hir::Ty {
1175+
kind,
1176+
span: self.lower_span(t.span),
1177+
hir_id: self.next_id(),
1178+
source: TySource::Other,
1179+
};
11751180
}
11761181

11771182
let id = self.lower_node_id(t.id);
@@ -1188,7 +1193,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11881193
}
11891194

11901195
fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1191-
hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
1196+
hir::Ty {
1197+
hir_id: self.next_id(),
1198+
kind,
1199+
span: self.lower_span(span),
1200+
source: TySource::Other,
1201+
}
11921202
}
11931203

11941204
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
@@ -1209,7 +1219,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12091219
let lifetime = self.lower_ty_direct_lifetime(t, *region);
12101220
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));
12111221
let span = self.lower_span(t.span);
1212-
let arg = hir::Ty { kind, span, hir_id: self.next_id() };
1222+
let arg = hir::Ty { kind, span, hir_id: self.next_id(), source: TySource::Other };
12131223
let args = self.arena.alloc(hir::GenericArgs {
12141224
args: self.arena.alloc([hir::GenericArg::Type(self.arena.alloc(arg))]),
12151225
constraints: &[],
@@ -1374,7 +1384,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13741384
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
13751385
};
13761386

1377-
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1387+
hir::Ty {
1388+
kind,
1389+
span: self.lower_span(t.span),
1390+
hir_id: self.lower_node_id(t.id),
1391+
source: TySource::Other,
1392+
}
13781393
}
13791394

13801395
fn lower_ty_direct_lifetime(
@@ -2375,7 +2390,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23752390
}
23762391
}
23772392

2378-
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2393+
fn ty_path(&mut self, hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
2394+
self.ty_path_with_source(hir_id, span, qpath, TySource::Other)
2395+
}
2396+
2397+
fn ty_path_with_source(
2398+
&mut self,
2399+
mut hir_id: HirId,
2400+
span: Span,
2401+
qpath: hir::QPath<'hir>,
2402+
source: TySource,
2403+
) -> hir::Ty<'hir> {
23792404
let kind = match qpath {
23802405
hir::QPath::Resolved(None, path) => {
23812406
// Turn trait object paths into `TyKind::TraitObject` instead.
@@ -2402,7 +2427,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24022427
_ => hir::TyKind::Path(qpath),
24032428
};
24042429

2405-
hir::Ty { hir_id, kind, span: self.lower_span(span) }
2430+
hir::Ty { hir_id, kind, span: self.lower_span(span), source }
24062431
}
24072432

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

compiler/rustc_ast_lowering/src/path.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use rustc_ast::{self as ast, *};
44
use rustc_hir::def::{DefKind, PartialRes, Res};
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::{self as hir, GenericArg};
6+
use rustc_hir::{self as hir, GenericArg, TySource};
77
use rustc_middle::span_bug;
88
use rustc_session::parse::add_feature_diagnostics;
99
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
@@ -169,7 +169,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
169169
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
170170
// `<I as Iterator>::Item::default`.
171171
let new_id = self.next_id();
172-
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+
))
173178
};
174179

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

compiler/rustc_hir/src/hir.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,12 @@ pub struct InferArg {
473473

474474
impl InferArg {
475475
pub fn to_ty(&self) -> Ty<'static> {
476-
Ty { kind: TyKind::Infer(()), span: self.span, hir_id: self.hir_id }
476+
Ty {
477+
kind: TyKind::Infer(()),
478+
span: self.span,
479+
hir_id: self.hir_id,
480+
source: TySource::Other,
481+
}
477482
}
478483
}
479484

@@ -3226,6 +3231,16 @@ impl<'hir> AssocItemConstraintKind<'hir> {
32263231
}
32273232
}
32283233

3234+
#[derive(Debug, Clone, Copy, PartialEq, HashStable_Generic)]
3235+
pub enum TySource {
3236+
/// `Vec` in `Vec::new`
3237+
ImplicitSelf,
3238+
3239+
/// Details not yet needed. Feel free to give useful
3240+
/// categorization to these usages.
3241+
Other,
3242+
}
3243+
32293244
/// An uninhabited enum used to make `Infer` variants on [`Ty`] and [`ConstArg`] be
32303245
/// unreachable. Zero-Variant enums are guaranteed to have the same layout as the never
32313246
/// type.
@@ -3245,6 +3260,7 @@ pub struct Ty<'hir, Unambig = ()> {
32453260
pub hir_id: HirId,
32463261
pub span: Span,
32473262
pub kind: TyKind<'hir, Unambig>,
3263+
pub source: TySource,
32483264
}
32493265

32503266
impl<'hir> Ty<'hir, AmbigArg> {
@@ -4943,7 +4959,7 @@ mod size_asserts {
49434959
static_assert_size!(StmtKind<'_>, 16);
49444960
static_assert_size!(TraitItem<'_>, 88);
49454961
static_assert_size!(TraitItemKind<'_>, 48);
4946-
static_assert_size!(Ty<'_>, 48);
4962+
static_assert_size!(Ty<'_>, 56);
49474963
static_assert_size!(TyKind<'_>, 32);
49484964
// tidy-alphabetical-end
49494965
}

compiler/rustc_hir/src/hir/tests.rs

+3-3
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

+1-1
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 {

tests/ui/stats/input-stats.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,29 @@ hir-stats Pat 360 ( 4.0%) 5 72
150150
hir-stats - Struct 72 ( 0.8%) 1
151151
hir-stats - Wild 72 ( 0.8%) 1
152152
hir-stats - Binding 216 ( 2.4%) 3
153-
hir-stats GenericParam 400 ( 4.5%) 5 80
154-
hir-stats Generics 560 ( 6.2%) 10 56
155-
hir-stats Ty 720 ( 8.0%) 15 48
156-
hir-stats - Ptr 48 ( 0.5%) 1
157-
hir-stats - Ref 48 ( 0.5%) 1
158-
hir-stats - Path 624 ( 6.9%) 13
159-
hir-stats Expr 768 ( 8.5%) 12 64
153+
hir-stats GenericParam 400 ( 4.4%) 5 80
154+
hir-stats Generics 560 ( 6.1%) 10 56
155+
hir-stats Expr 768 ( 8.4%) 12 64
160156
hir-stats - InlineAsm 64 ( 0.7%) 1
161157
hir-stats - Match 64 ( 0.7%) 1
162158
hir-stats - Path 64 ( 0.7%) 1
163159
hir-stats - Struct 64 ( 0.7%) 1
164160
hir-stats - Lit 128 ( 1.4%) 2
165-
hir-stats - Block 384 ( 4.3%) 6
166-
hir-stats Item 968 (10.8%) 11 88
161+
hir-stats - Block 384 ( 4.2%) 6
162+
hir-stats Ty 840 ( 9.2%) 15 56
163+
hir-stats - Ptr 56 ( 0.6%) 1
164+
hir-stats - Ref 56 ( 0.6%) 1
165+
hir-stats - Path 728 ( 8.0%) 13
166+
hir-stats Item 968 (10.6%) 11 88
167167
hir-stats - Enum 88 ( 1.0%) 1
168168
hir-stats - ExternCrate 88 ( 1.0%) 1
169169
hir-stats - ForeignMod 88 ( 1.0%) 1
170170
hir-stats - Impl 88 ( 1.0%) 1
171171
hir-stats - Trait 88 ( 1.0%) 1
172-
hir-stats - Fn 176 ( 2.0%) 2
172+
hir-stats - Fn 176 ( 1.9%) 2
173173
hir-stats - Use 352 ( 3.9%) 4
174-
hir-stats Path 1_240 (13.8%) 31 40
175-
hir-stats PathSegment 1_920 (21.4%) 40 48
174+
hir-stats Path 1_240 (13.6%) 31 40
175+
hir-stats PathSegment 1_920 (21.1%) 40 48
176176
hir-stats ----------------------------------------------------------------
177-
hir-stats Total 8_988 180
177+
hir-stats Total 9_108 180
178178
hir-stats

0 commit comments

Comments
 (0)