Skip to content

Commit c8a331a

Browse files
committed
Unify optional param info with object lifetime default boolean into an enum that exhaustively supports all call sites
1 parent 4146b82 commit c8a331a

File tree

8 files changed

+75
-53
lines changed

8 files changed

+75
-53
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::ops::Bound;
4444

4545
use crate::check::intrinsic::intrinsic_operation_unsafety;
4646
use crate::errors;
47-
use crate::hir_ty_lowering::HirTyLowerer;
47+
use crate::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
4848
pub use type_of::test_opaque_hidden_types;
4949

5050
mod generics_of;
@@ -374,13 +374,8 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
374374
self.item_def_id
375375
}
376376

377-
fn re_infer(
378-
&self,
379-
_: Option<&ty::GenericParamDef>,
380-
span: Span,
381-
object_lifetime_default: bool,
382-
) -> ty::Region<'tcx> {
383-
if object_lifetime_default {
377+
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
378+
if let RegionInferReason::BorrowedObjectLifetimeDefault = reason {
384379
let e = struct_span_code_err!(
385380
self.tcx().dcx(),
386381
span,

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::bounds::Bounds;
22
use crate::collect::ItemCtxt;
33
use crate::constrained_generic_params as cgp;
4-
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter};
4+
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason};
55
use hir::{HirId, Node};
66
use rustc_data_structures::fx::FxIndexSet;
77
use rustc_hir as hir;
@@ -243,12 +243,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
243243
}
244244

245245
hir::WherePredicate::RegionPredicate(region_pred) => {
246-
let r1 = icx.lowerer().lower_lifetime(region_pred.lifetime, None);
246+
let r1 = icx
247+
.lowerer()
248+
.lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
247249
predicates.extend(region_pred.bounds.iter().map(|bound| {
248250
let (r2, span) = match bound {
249-
hir::GenericBound::Outlives(lt) => {
250-
(icx.lowerer().lower_lifetime(lt, None), lt.ident.span)
251-
}
251+
hir::GenericBound::Outlives(lt) => (
252+
icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
253+
lt.ident.span,
254+
),
252255
bound => {
253256
span_bug!(
254257
bound.span(),

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::bounds::Bounds;
1818
use crate::errors;
1919
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter};
2020

21+
use super::RegionInferReason;
22+
2123
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2224
/// Add a `Sized` bound to the `bounds` if appropriate.
2325
///
@@ -166,7 +168,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
166168
);
167169
}
168170
hir::GenericBound::Outlives(lifetime) => {
169-
let region = self.lower_lifetime(lifetime, None);
171+
let region = self.lower_lifetime(lifetime, RegionInferReason::OutlivesBound);
170172
bounds.push_region_bound(
171173
self.tcx(),
172174
ty::Binder::bind_with_vars(

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ pub enum PredicateFilter {
8080
SelfAndAssociatedTypeBounds,
8181
}
8282

83+
#[derive(Debug)]
84+
pub enum RegionInferReason<'a> {
85+
/// Lifetime on a trait object behind a reference.
86+
/// This allows inferring information from the reference.
87+
BorrowedObjectLifetimeDefault,
88+
/// A trait object's lifetime.
89+
ObjectLifetimeDefault,
90+
/// Generic lifetime parameter
91+
Param(&'a ty::GenericParamDef),
92+
RegionPredicate,
93+
Reference,
94+
OutlivesBound,
95+
}
96+
8397
/// A context which can lower type-system entities from the [HIR][hir] to
8498
/// the [`rustc_middle::ty`] representation.
8599
///
@@ -91,14 +105,7 @@ pub trait HirTyLowerer<'tcx> {
91105
fn item_def_id(&self) -> LocalDefId;
92106

93107
/// Returns the region to use when a lifetime is omitted (and not elided).
94-
///
95-
/// The `object_lifetime_default` argument states whether this lifetime is from a reference.
96-
fn re_infer(
97-
&self,
98-
param: Option<&ty::GenericParamDef>,
99-
span: Span,
100-
object_lifetime_default: bool,
101-
) -> ty::Region<'tcx>;
108+
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx>;
102109

103110
/// Returns the type to use when a type is omitted.
104111
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
@@ -267,7 +274,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
267274
pub fn lower_lifetime(
268275
&self,
269276
lifetime: &hir::Lifetime,
270-
def: Option<&ty::GenericParamDef>,
277+
reason: RegionInferReason<'_>,
271278
) -> ty::Region<'tcx> {
272279
let tcx = self.tcx();
273280
let lifetime_name = |def_id| tcx.hir().name(tcx.local_def_id_to_hir_id(def_id));
@@ -301,7 +308,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
301308

302309
Some(rbv::ResolvedArg::Error(guar)) => ty::Region::new_error(tcx, guar),
303310

304-
None => self.re_infer(def, lifetime.ident.span, false),
311+
None => self.re_infer(lifetime.ident.span, reason),
305312
}
306313
}
307314

@@ -466,7 +473,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
466473

467474
match (&param.kind, arg) {
468475
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
469-
self.lowerer.lower_lifetime(lt, Some(param)).into()
476+
self.lowerer.lower_lifetime(lt, RegionInferReason::Param(param)).into()
470477
}
471478
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
472479
handle_ty_args(has_default, ty)
@@ -509,7 +516,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
509516
}
510517
match param.kind {
511518
GenericParamDefKind::Lifetime => {
512-
self.lowerer.re_infer(Some(param), self.span, false).into()
519+
self.lowerer.re_infer(self.span, RegionInferReason::Param(param)).into()
513520
}
514521
GenericParamDefKind::Type { has_default, .. } => {
515522
if !infer_args && has_default {
@@ -2041,7 +2048,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20412048
hir::TyKind::Slice(ty) => Ty::new_slice(tcx, self.lower_ty(ty)),
20422049
hir::TyKind::Ptr(mt) => Ty::new_ptr(tcx, self.lower_ty(mt.ty), mt.mutbl),
20432050
hir::TyKind::Ref(region, mt) => {
2044-
let r = self.lower_lifetime(region, None);
2051+
let r = self.lower_lifetime(region, RegionInferReason::Reference);
20452052
debug!(?r);
20462053
let t = self.lower_ty_common(mt.ty, true, false);
20472054
Ty::new_ref(tcx, r, t, mt.mutbl)
@@ -2270,7 +2277,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22702277
&lifetimes[i]
22712278
)
22722279
};
2273-
self.lower_lifetime(lifetime, None).into()
2280+
self.lower_lifetime(lifetime, RegionInferReason::Param(&param)).into()
22742281
} else {
22752282
tcx.mk_param_from_def(param)
22762283
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::bounds::Bounds;
2-
use crate::hir_ty_lowering::{GenericArgCountMismatch, GenericArgCountResult, OnlySelfBounds};
2+
use crate::hir_ty_lowering::{
3+
GenericArgCountMismatch, GenericArgCountResult, OnlySelfBounds, RegionInferReason,
4+
};
35
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
46
use rustc_errors::{codes::*, struct_span_code_err};
57
use rustc_hir as hir;
@@ -321,13 +323,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
321323

322324
// Use explicitly-specified region bound.
323325
let region_bound = if !lifetime.is_elided() {
324-
self.lower_lifetime(lifetime, None)
326+
self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault)
325327
} else {
326328
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
327329
if tcx.named_bound_var(lifetime.hir_id).is_some() {
328-
self.lower_lifetime(lifetime, None)
330+
self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault)
329331
} else {
330-
self.re_infer(None, span, !borrowed)
332+
self.re_infer(
333+
span,
334+
if borrowed {
335+
RegionInferReason::ObjectLifetimeDefault
336+
} else {
337+
RegionInferReason::BorrowedObjectLifetimeDefault
338+
},
339+
)
331340
}
332341
})
333342
};

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir_analysis::hir_ty_lowering::generics::{
1616
};
1717
use rustc_hir_analysis::hir_ty_lowering::{
1818
ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgsLowerer,
19-
GenericPathSegment, HirTyLowerer, IsMethodCall,
19+
GenericPathSegment, HirTyLowerer, IsMethodCall, RegionInferReason,
2020
};
2121
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
2222
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
@@ -1280,9 +1280,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12801280
arg: &GenericArg<'tcx>,
12811281
) -> ty::GenericArg<'tcx> {
12821282
match (&param.kind, arg) {
1283-
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
1284-
self.fcx.lowerer().lower_lifetime(lt, Some(param)).into()
1285-
}
1283+
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => self
1284+
.fcx
1285+
.lowerer()
1286+
.lower_lifetime(lt, RegionInferReason::Param(param))
1287+
.into(),
12861288
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
12871289
self.fcx.lower_ty(ty).raw.into()
12881290
}
@@ -1324,9 +1326,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13241326
) -> ty::GenericArg<'tcx> {
13251327
let tcx = self.fcx.tcx();
13261328
match param.kind {
1327-
GenericParamDefKind::Lifetime => {
1328-
self.fcx.re_infer(Some(param), self.span, false).into()
1329-
}
1329+
GenericParamDefKind::Lifetime => self
1330+
.fcx
1331+
.re_infer(
1332+
self.span,
1333+
rustc_hir_analysis::hir_ty_lowering::RegionInferReason::Param(param),
1334+
)
1335+
.into(),
13301336
GenericParamDefKind::Type { has_default, .. } => {
13311337
if !infer_args && has_default {
13321338
// If we have a default, then it doesn't matter that we're not

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir::def_id::CRATE_DEF_ID;
1515
use rustc_errors::DiagCtxt;
1616
use rustc_hir as hir;
1717
use rustc_hir::def_id::{DefId, LocalDefId};
18-
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
18+
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
1919
use rustc_infer::infer;
2020
use rustc_infer::infer::error_reporting::sub_relations::SubRelations;
2121
use rustc_infer::infer::error_reporting::TypeErrCtxt;
@@ -222,15 +222,10 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
222222
self.body_id
223223
}
224224

225-
fn re_infer(
226-
&self,
227-
def: Option<&ty::GenericParamDef>,
228-
span: Span,
229-
_object_lifetime_default: bool,
230-
) -> ty::Region<'tcx> {
231-
let v = match def {
232-
Some(def) => infer::RegionParameterDefinition(span, def.name),
233-
None => infer::MiscVariable(span),
225+
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
226+
let v = match reason {
227+
RegionInferReason::Param(def) => infer::RegionParameterDefinition(span, def.name),
228+
_ => infer::MiscVariable(span),
234229
};
235230
self.next_region_var(v)
236231
}

compiler/rustc_hir_typeck/src/method/confirm.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_hir::GenericArg;
77
use rustc_hir_analysis::hir_ty_lowering::generics::{
88
check_generic_arg_count_for_call, lower_generic_args,
99
};
10-
use rustc_hir_analysis::hir_ty_lowering::{GenericArgsLowerer, HirTyLowerer, IsMethodCall};
10+
use rustc_hir_analysis::hir_ty_lowering::{
11+
GenericArgsLowerer, HirTyLowerer, IsMethodCall, RegionInferReason,
12+
};
1113
use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk};
1214
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
1315
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
@@ -388,9 +390,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
388390
arg: &GenericArg<'tcx>,
389391
) -> ty::GenericArg<'tcx> {
390392
match (&param.kind, arg) {
391-
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
392-
self.cfcx.fcx.lowerer().lower_lifetime(lt, Some(param)).into()
393-
}
393+
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => self
394+
.cfcx
395+
.fcx
396+
.lowerer()
397+
.lower_lifetime(lt, RegionInferReason::Param(param))
398+
.into(),
394399
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
395400
self.cfcx.lower_ty(ty).raw.into()
396401
}

0 commit comments

Comments
 (0)