Skip to content

Commit 867d39c

Browse files
committed
Auto merge of rust-lang#120100 - oli-obk:astconv_lifetimes, r=BoxyUwU
Don't forget that the lifetime on hir types is `'tcx` This PR just tracks the `'tcx` lifetime to wherever the original objects actually have that lifetime. This code is needed for rust-lang#107606 (now rust-lang#120131) so that `ast_ty_to_ty` can invoke `lit_to_const` on an argument passed to it. Currently the argument is `&hir::Ty<'_>`, but after this PR it is `&'tcx hir::Ty<'tcx>`.
2 parents 4cb17b4 + e532b0d commit 867d39c

26 files changed

+105
-98
lines changed

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub struct InferArg {
248248
}
249249

250250
impl InferArg {
251-
pub fn to_ty(&self) -> Ty<'_> {
251+
pub fn to_ty(&self) -> Ty<'static> {
252252
Ty { kind: TyKind::Infer, span: self.span, hir_id: self.hir_id }
253253
}
254254
}

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
108108
/// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref`
109109
/// for more details.
110110
#[instrument(level = "debug", skip(self, ast_bounds, bounds))]
111-
pub(crate) fn add_bounds<'hir, I: Iterator<Item = &'hir hir::GenericBound<'hir>>>(
111+
pub(crate) fn add_bounds<'hir, I: Iterator<Item = &'hir hir::GenericBound<'tcx>>>(
112112
&self,
113113
param_ty: Ty<'tcx>,
114114
ast_bounds: I,
115115
bounds: &mut Bounds<'tcx>,
116116
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
117117
only_self_bounds: OnlySelfBounds,
118-
) {
118+
) where
119+
'tcx: 'hir,
120+
{
119121
for ast_bound in ast_bounds {
120122
match ast_bound {
121123
hir::GenericBound::Trait(poly_trait_ref, modifier) => {
@@ -179,7 +181,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
179181
pub(crate) fn compute_bounds(
180182
&self,
181183
param_ty: Ty<'tcx>,
182-
ast_bounds: &[hir::GenericBound<'_>],
184+
ast_bounds: &[hir::GenericBound<'tcx>],
183185
filter: PredicateFilter,
184186
) -> Bounds<'tcx> {
185187
let mut bounds = Bounds::default();

compiler/rustc_hir_analysis/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn generic_arg_mismatch_err(
168168
/// instantiate a `GenericArg`.
169169
/// - `inferred_kind`: if no parameter was provided, and inference is enabled, then
170170
/// creates a suitable inference variable.
171-
pub fn create_args_for_parent_generic_args<'tcx, 'a>(
171+
pub fn create_args_for_parent_generic_args<'tcx: 'a, 'a>(
172172
tcx: TyCtxt<'tcx>,
173173
def_id: DefId,
174174
parent_args: &[ty::GenericArg<'tcx>],

compiler/rustc_hir_analysis/src/astconv/mod.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub trait AstConv<'tcx> {
122122
&self,
123123
span: Span,
124124
item_def_id: DefId,
125-
item_segment: &hir::PathSegment<'_>,
125+
item_segment: &hir::PathSegment<'tcx>,
126126
poly_trait_ref: ty::PolyTraitRef<'tcx>,
127127
) -> Ty<'tcx>;
128128

@@ -156,14 +156,14 @@ struct ConvertedBinding<'a, 'tcx> {
156156
hir_id: hir::HirId,
157157
item_name: Ident,
158158
kind: ConvertedBindingKind<'a, 'tcx>,
159-
gen_args: &'a GenericArgs<'a>,
159+
gen_args: &'tcx GenericArgs<'tcx>,
160160
span: Span,
161161
}
162162

163163
#[derive(Debug)]
164164
enum ConvertedBindingKind<'a, 'tcx> {
165165
Equality(Spanned<ty::Term<'tcx>>),
166-
Constraint(&'a [hir::GenericBound<'a>]),
166+
Constraint(&'a [hir::GenericBound<'tcx>]),
167167
}
168168

169169
/// New-typed boolean indicating whether explicit late-bound lifetimes
@@ -215,12 +215,12 @@ pub struct GenericArgCountResult {
215215
}
216216

217217
pub trait CreateSubstsForGenericArgsCtxt<'a, 'tcx> {
218-
fn args_for_def_id(&mut self, def_id: DefId) -> (Option<&'a GenericArgs<'a>>, bool);
218+
fn args_for_def_id(&mut self, def_id: DefId) -> (Option<&'a GenericArgs<'tcx>>, bool);
219219

220220
fn provided_kind(
221221
&mut self,
222222
param: &ty::GenericParamDef,
223-
arg: &GenericArg<'_>,
223+
arg: &GenericArg<'tcx>,
224224
) -> ty::GenericArg<'tcx>;
225225

226226
fn inferred_kind(
@@ -294,7 +294,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
294294
&self,
295295
span: Span,
296296
def_id: DefId,
297-
item_segment: &hir::PathSegment<'_>,
297+
item_segment: &hir::PathSegment<'tcx>,
298298
) -> GenericArgsRef<'tcx> {
299299
let (args, _) = self.create_args_for_ast_path(
300300
span,
@@ -351,7 +351,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
351351
def_id: DefId,
352352
parent_args: &[ty::GenericArg<'tcx>],
353353
seg: &hir::PathSegment<'_>,
354-
generic_args: &'a hir::GenericArgs<'_>,
354+
generic_args: &'a hir::GenericArgs<'tcx>,
355355
infer_args: bool,
356356
self_ty: Option<Ty<'tcx>>,
357357
constness: ty::BoundConstness,
@@ -406,14 +406,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
406406
struct SubstsForAstPathCtxt<'a, 'tcx> {
407407
astconv: &'a (dyn AstConv<'tcx> + 'a),
408408
def_id: DefId,
409-
generic_args: &'a GenericArgs<'a>,
409+
generic_args: &'a GenericArgs<'tcx>,
410410
span: Span,
411411
inferred_params: Vec<Span>,
412412
infer_args: bool,
413413
}
414414

415415
impl<'a, 'tcx> CreateSubstsForGenericArgsCtxt<'a, 'tcx> for SubstsForAstPathCtxt<'a, 'tcx> {
416-
fn args_for_def_id(&mut self, did: DefId) -> (Option<&'a GenericArgs<'a>>, bool) {
416+
fn args_for_def_id(&mut self, did: DefId) -> (Option<&'a GenericArgs<'tcx>>, bool) {
417417
if did == self.def_id {
418418
(Some(self.generic_args), self.infer_args)
419419
} else {
@@ -425,11 +425,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
425425
fn provided_kind(
426426
&mut self,
427427
param: &ty::GenericParamDef,
428-
arg: &GenericArg<'_>,
428+
arg: &GenericArg<'tcx>,
429429
) -> ty::GenericArg<'tcx> {
430430
let tcx = self.astconv.tcx();
431431

432-
let mut handle_ty_args = |has_default, ty: &hir::Ty<'_>| {
432+
let mut handle_ty_args = |has_default, ty: &hir::Ty<'tcx>| {
433433
if has_default {
434434
tcx.check_optional_stability(
435435
param.def_id,
@@ -592,7 +592,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
592592

593593
fn create_assoc_bindings_for_generic_args<'a>(
594594
&self,
595-
generic_args: &'a hir::GenericArgs<'_>,
595+
generic_args: &'a hir::GenericArgs<'tcx>,
596596
) -> Vec<ConvertedBinding<'a, 'tcx>> {
597597
// Convert associated-type bindings or constraints into a separate vector.
598598
// Example: Given this:
@@ -640,7 +640,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
640640
&self,
641641
span: Span,
642642
item_def_id: DefId,
643-
item_segment: &hir::PathSegment<'_>,
643+
item_segment: &hir::PathSegment<'tcx>,
644644
parent_args: GenericArgsRef<'tcx>,
645645
) -> GenericArgsRef<'tcx> {
646646
debug!(
@@ -673,7 +673,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
673673
/// are disallowed. Otherwise, they are pushed onto the vector given.
674674
pub fn instantiate_mono_trait_ref(
675675
&self,
676-
trait_ref: &hir::TraitRef<'_>,
676+
trait_ref: &hir::TraitRef<'tcx>,
677677
self_ty: Ty<'tcx>,
678678
) -> ty::TraitRef<'tcx> {
679679
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
@@ -710,7 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
710710
#[instrument(level = "debug", skip(self, span, constness, bounds, speculative))]
711711
pub(crate) fn instantiate_poly_trait_ref(
712712
&self,
713-
trait_ref: &hir::TraitRef<'_>,
713+
trait_ref: &hir::TraitRef<'tcx>,
714714
span: Span,
715715
constness: ty::BoundConstness,
716716
polarity: ty::ImplPolarity,
@@ -788,7 +788,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
788788
span: Span,
789789
trait_def_id: DefId,
790790
self_ty: Ty<'tcx>,
791-
trait_segment: &hir::PathSegment<'_>,
791+
trait_segment: &hir::PathSegment<'tcx>,
792792
is_impl: bool,
793793
// FIXME(effects) move all host param things in astconv to hir lowering
794794
constness: ty::BoundConstness,
@@ -813,7 +813,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
813813
span: Span,
814814
trait_def_id: DefId,
815815
self_ty: Ty<'tcx>,
816-
trait_segment: &'a hir::PathSegment<'a>,
816+
trait_segment: &'a hir::PathSegment<'tcx>,
817817
is_impl: bool,
818818
constness: ty::BoundConstness,
819819
) -> (GenericArgsRef<'tcx>, GenericArgCountResult) {
@@ -847,7 +847,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
847847
&self,
848848
span: Span,
849849
did: DefId,
850-
item_segment: &hir::PathSegment<'_>,
850+
item_segment: &hir::PathSegment<'tcx>,
851851
) -> Ty<'tcx> {
852852
let tcx = self.tcx();
853853
let args = self.ast_path_args_for_ty(span, did, item_segment);
@@ -1153,7 +1153,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11531153
span: Span,
11541154
qself_ty: Ty<'tcx>,
11551155
qself: &hir::Ty<'_>,
1156-
assoc_segment: &hir::PathSegment<'_>,
1156+
assoc_segment: &hir::PathSegment<'tcx>,
11571157
permit_variants: bool,
11581158
) -> Result<(Ty<'tcx>, DefKind, DefId), ErrorGuaranteed> {
11591159
let tcx = self.tcx();
@@ -1428,7 +1428,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14281428
fn lookup_inherent_assoc_ty(
14291429
&self,
14301430
name: Ident,
1431-
segment: &hir::PathSegment<'_>,
1431+
segment: &hir::PathSegment<'tcx>,
14321432
adt_did: DefId,
14331433
self_ty: Ty<'tcx>,
14341434
block: hir::HirId,
@@ -1702,8 +1702,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
17021702
span: Span,
17031703
opt_self_ty: Option<Ty<'tcx>>,
17041704
item_def_id: DefId,
1705-
trait_segment: &hir::PathSegment<'_>,
1706-
item_segment: &hir::PathSegment<'_>,
1705+
trait_segment: &hir::PathSegment<'tcx>,
1706+
item_segment: &hir::PathSegment<'tcx>,
17071707
constness: ty::BoundConstness,
17081708
) -> Ty<'tcx> {
17091709
let tcx = self.tcx();
@@ -2021,7 +2021,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20212021
pub fn res_to_ty(
20222022
&self,
20232023
opt_self_ty: Option<Ty<'tcx>>,
2024-
path: &hir::Path<'_>,
2024+
path: &hir::Path<'tcx>,
20252025
hir_id: hir::HirId,
20262026
permit_variants: bool,
20272027
) -> Ty<'tcx> {
@@ -2311,13 +2311,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23112311

23122312
/// Parses the programmer's textual representation of a type into our
23132313
/// internal notion of a type.
2314-
pub fn ast_ty_to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
2314+
pub fn ast_ty_to_ty(&self, ast_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
23152315
self.ast_ty_to_ty_inner(ast_ty, false, false)
23162316
}
23172317

23182318
/// Parses the programmer's textual representation of a type into our
23192319
/// internal notion of a type. This is meant to be used within a path.
2320-
pub fn ast_ty_to_ty_in_path(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
2320+
pub fn ast_ty_to_ty_in_path(&self, ast_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
23212321
self.ast_ty_to_ty_inner(ast_ty, false, true)
23222322
}
23232323

@@ -2432,7 +2432,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24322432
/// Turns a `hir::Ty` into a `Ty`. For diagnostics' purposes we keep track of whether trait
24332433
/// objects are borrowed like `&dyn Trait` to avoid emitting redundant errors.
24342434
#[instrument(level = "debug", skip(self), ret)]
2435-
fn ast_ty_to_ty_inner(&self, ast_ty: &hir::Ty<'_>, borrowed: bool, in_path: bool) -> Ty<'tcx> {
2435+
fn ast_ty_to_ty_inner(
2436+
&self,
2437+
ast_ty: &hir::Ty<'tcx>,
2438+
borrowed: bool,
2439+
in_path: bool,
2440+
) -> Ty<'tcx> {
24362441
let tcx = self.tcx();
24372442

24382443
let result_ty = match &ast_ty.kind {
@@ -2609,7 +2614,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26092614
}
26102615
}
26112616

2612-
pub fn ty_of_arg(&self, ty: &hir::Ty<'_>, expected_ty: Option<Ty<'tcx>>) -> Ty<'tcx> {
2617+
pub fn ty_of_arg(&self, ty: &hir::Ty<'tcx>, expected_ty: Option<Ty<'tcx>>) -> Ty<'tcx> {
26132618
match ty.kind {
26142619
hir::TyKind::Infer if expected_ty.is_some() => {
26152620
self.record_ty(ty.hir_id, expected_ty.unwrap(), ty.span);
@@ -2625,7 +2630,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26252630
hir_id: hir::HirId,
26262631
unsafety: hir::Unsafety,
26272632
abi: abi::Abi,
2628-
decl: &hir::FnDecl<'_>,
2633+
decl: &hir::FnDecl<'tcx>,
26292634
generics: Option<&hir::Generics<'_>>,
26302635
hir_ty: Option<&hir::Ty<'_>>,
26312636
) -> ty::PolyFnSig<'tcx> {

compiler/rustc_hir_analysis/src/astconv/object_safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2222
&self,
2323
span: Span,
2424
hir_id: hir::HirId,
25-
hir_trait_bounds: &[hir::PolyTraitRef<'_>],
25+
hir_trait_bounds: &[hir::PolyTraitRef<'tcx>],
2626
lifetime: &hir::Lifetime,
2727
borrowed: bool,
2828
representation: DynKind,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ fn receiver_is_implemented<'tcx>(
17901790
fn check_variances_for_type_defn<'tcx>(
17911791
tcx: TyCtxt<'tcx>,
17921792
item: &hir::Item<'tcx>,
1793-
hir_generics: &hir::Generics<'_>,
1793+
hir_generics: &hir::Generics<'tcx>,
17941794
) {
17951795
let identity_args = ty::GenericArgs::identity_for_item(tcx, item.owner_id);
17961796

compiler/rustc_hir_analysis/src/collect.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'tcx> ItemCtxt<'tcx> {
348348
ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
349349
}
350350

351-
pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
351+
pub fn to_ty(&self, ast_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
352352
self.astconv().ast_ty_to_ty(ast_ty)
353353
}
354354

@@ -412,7 +412,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
412412
&self,
413413
span: Span,
414414
item_def_id: DefId,
415-
item_segment: &hir::PathSegment<'_>,
415+
item_segment: &hir::PathSegment<'tcx>,
416416
poly_trait_ref: ty::PolyTraitRef<'tcx>,
417417
) -> Ty<'tcx> {
418418
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
@@ -1148,7 +1148,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
11481148

11491149
fn infer_return_ty_for_fn_sig<'tcx>(
11501150
tcx: TyCtxt<'tcx>,
1151-
sig: &hir::FnSig<'_>,
1151+
sig: &hir::FnSig<'tcx>,
11521152
generics: &hir::Generics<'_>,
11531153
def_id: LocalDefId,
11541154
icx: &ItemCtxt<'tcx>,
@@ -1352,14 +1352,14 @@ fn impl_trait_ref(
13521352
let last_arg = args.args.len() - 1;
13531353
assert!(matches!(args.args[last_arg], hir::GenericArg::Const(anon_const) if anon_const.is_desugared_from_effects));
13541354
args.args = &args.args[..args.args.len() - 1];
1355-
path_segments[last_segment].args = Some(&args);
1355+
path_segments[last_segment].args = Some(tcx.hir_arena.alloc(args));
13561356
let path = hir::Path {
13571357
span: ast_trait_ref.path.span,
13581358
res: ast_trait_ref.path.res,
1359-
segments: &path_segments,
1359+
segments: tcx.hir_arena.alloc_slice(&path_segments),
13601360
};
1361-
let trait_ref = hir::TraitRef { path: &path, hir_ref_id: ast_trait_ref.hir_ref_id };
1362-
icx.astconv().instantiate_mono_trait_ref(&trait_ref, selfty)
1361+
let trait_ref = tcx.hir_arena.alloc(hir::TraitRef { path: tcx.hir_arena.alloc(path), hir_ref_id: ast_trait_ref.hir_ref_id });
1362+
icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty)
13631363
} else {
13641364
icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty)
13651365
}

compiler/rustc_hir_analysis/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
221221

222222
/// A quasi-deprecated helper used in rustdoc and clippy to get
223223
/// the type from a HIR node.
224-
pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
224+
pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
225225
// In case there are any projections, etc., find the "environment"
226226
// def-ID that will be used to determine the traits/predicates in
227227
// scope. This is derived from the enclosing item-like thing.
@@ -232,7 +232,7 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
232232

233233
pub fn hir_trait_to_predicates<'tcx>(
234234
tcx: TyCtxt<'tcx>,
235-
hir_trait: &hir::TraitRef<'_>,
235+
hir_trait: &hir::TraitRef<'tcx>,
236236
self_ty: Ty<'tcx>,
237237
) -> Bounds<'tcx> {
238238
// In case there are any projections, etc., find the "environment"

0 commit comments

Comments
 (0)