Skip to content

Commit

Permalink
Unify optional param info with object lifetime default boolean into a…
Browse files Browse the repository at this point in the history
…n enum that exhaustively supports all call sites
  • Loading branch information
oli-obk committed Jun 4, 2024
1 parent a0ef311 commit ac117f0
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 53 deletions.
11 changes: 3 additions & 8 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::ops::Bound;

use crate::check::intrinsic::intrinsic_operation_unsafety;
use crate::errors;
use crate::hir_ty_lowering::HirTyLowerer;
use crate::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
pub use type_of::test_opaque_hidden_types;

mod generics_of;
Expand Down Expand Up @@ -374,13 +374,8 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
self.item_def_id
}

fn re_infer(
&self,
_: Option<&ty::GenericParamDef>,
span: Span,
object_lifetime_default: bool,
) -> ty::Region<'tcx> {
if object_lifetime_default {
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
if let RegionInferReason::BorrowedObjectLifetimeDefault = reason {
let e = struct_span_code_err!(
self.tcx().dcx(),
span,
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::bounds::Bounds;
use crate::collect::ItemCtxt;
use crate::constrained_generic_params as cgp;
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter};
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason};
use hir::{HirId, Node};
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir;
Expand Down Expand Up @@ -243,12 +243,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
}

hir::WherePredicate::RegionPredicate(region_pred) => {
let r1 = icx.lowerer().lower_lifetime(region_pred.lifetime, None);
let r1 = icx
.lowerer()
.lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
predicates.extend(region_pred.bounds.iter().map(|bound| {
let (r2, span) = match bound {
hir::GenericBound::Outlives(lt) => {
(icx.lowerer().lower_lifetime(lt, None), lt.ident.span)
}
hir::GenericBound::Outlives(lt) => (
icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
lt.ident.span,
),
bound => {
span_bug!(
bound.span(),
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::bounds::Bounds;
use crate::errors;
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter};

use super::RegionInferReason;

impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
/// Add a `Sized` bound to the `bounds` if appropriate.
///
Expand Down Expand Up @@ -166,7 +168,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}
hir::GenericBound::Outlives(lifetime) => {
let region = self.lower_lifetime(lifetime, None);
let region = self.lower_lifetime(lifetime, RegionInferReason::OutlivesBound);
bounds.push_region_bound(
self.tcx(),
ty::Binder::bind_with_vars(
Expand Down
35 changes: 21 additions & 14 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ pub enum PredicateFilter {
SelfAndAssociatedTypeBounds,
}

#[derive(Debug)]
pub enum RegionInferReason<'a> {
/// Lifetime on a trait object behind a reference.
/// This allows inferring information from the reference.
BorrowedObjectLifetimeDefault,
/// A trait object's lifetime.
ObjectLifetimeDefault,
/// Generic lifetime parameter
Param(&'a ty::GenericParamDef),
RegionPredicate,
Reference,
OutlivesBound,
}

/// A context which can lower type-system entities from the [HIR][hir] to
/// the [`rustc_middle::ty`] representation.
///
Expand All @@ -91,14 +105,7 @@ pub trait HirTyLowerer<'tcx> {
fn item_def_id(&self) -> LocalDefId;

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

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

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

None => self.re_infer(def, lifetime.ident.span, false),
None => self.re_infer(lifetime.ident.span, reason),
}
}

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

match (&param.kind, arg) {
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
self.lowerer.lower_lifetime(lt, Some(param)).into()
self.lowerer.lower_lifetime(lt, RegionInferReason::Param(param)).into()
}
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
handle_ty_args(has_default, ty)
Expand Down Expand Up @@ -509,7 +516,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
match param.kind {
GenericParamDefKind::Lifetime => {
self.lowerer.re_infer(Some(param), self.span, false).into()
self.lowerer.re_infer(self.span, RegionInferReason::Param(param)).into()
}
GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default {
Expand Down Expand Up @@ -2041,7 +2048,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
hir::TyKind::Slice(ty) => Ty::new_slice(tcx, self.lower_ty(ty)),
hir::TyKind::Ptr(mt) => Ty::new_ptr(tcx, self.lower_ty(mt.ty), mt.mutbl),
hir::TyKind::Ref(region, mt) => {
let r = self.lower_lifetime(region, None);
let r = self.lower_lifetime(region, RegionInferReason::Reference);
debug!(?r);
let t = self.lower_ty_common(mt.ty, true, false);
Ty::new_ref(tcx, r, t, mt.mutbl)
Expand Down Expand Up @@ -2270,7 +2277,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&lifetimes[i]
)
};
self.lower_lifetime(lifetime, None).into()
self.lower_lifetime(lifetime, RegionInferReason::Param(&param)).into()
} else {
tcx.mk_param_from_def(param)
}
Expand Down
17 changes: 13 additions & 4 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::bounds::Bounds;
use crate::hir_ty_lowering::{GenericArgCountMismatch, GenericArgCountResult, OnlySelfBounds};
use crate::hir_ty_lowering::{
GenericArgCountMismatch, GenericArgCountResult, OnlySelfBounds, RegionInferReason,
};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::{codes::*, struct_span_code_err};
use rustc_hir as hir;
Expand Down Expand Up @@ -321,13 +323,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {

// Use explicitly-specified region bound.
let region_bound = if !lifetime.is_elided() {
self.lower_lifetime(lifetime, None)
self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault)
} else {
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
if tcx.named_bound_var(lifetime.hir_id).is_some() {
self.lower_lifetime(lifetime, None)
self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault)
} else {
self.re_infer(None, span, !borrowed)
self.re_infer(
span,
if borrowed {
RegionInferReason::ObjectLifetimeDefault
} else {
RegionInferReason::BorrowedObjectLifetimeDefault
},
)
}
})
};
Expand Down
20 changes: 13 additions & 7 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir_analysis::hir_ty_lowering::generics::{
};
use rustc_hir_analysis::hir_ty_lowering::{
ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgsLowerer,
GenericPathSegment, HirTyLowerer, IsMethodCall,
GenericPathSegment, HirTyLowerer, IsMethodCall, RegionInferReason,
};
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
Expand Down Expand Up @@ -1280,9 +1280,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
arg: &GenericArg<'tcx>,
) -> ty::GenericArg<'tcx> {
match (&param.kind, arg) {
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
self.fcx.lowerer().lower_lifetime(lt, Some(param)).into()
}
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => self
.fcx
.lowerer()
.lower_lifetime(lt, RegionInferReason::Param(param))
.into(),
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
self.fcx.lower_ty(ty).raw.into()
}
Expand Down Expand Up @@ -1324,9 +1326,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> ty::GenericArg<'tcx> {
let tcx = self.fcx.tcx();
match param.kind {
GenericParamDefKind::Lifetime => {
self.fcx.re_infer(Some(param), self.span, false).into()
}
GenericParamDefKind::Lifetime => self
.fcx
.re_infer(
self.span,
rustc_hir_analysis::hir_ty_lowering::RegionInferReason::Param(param),
)
.into(),
GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default {
// If we have a default, then it doesn't matter that we're not
Expand Down
15 changes: 5 additions & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hir::def_id::CRATE_DEF_ID;
use rustc_errors::DiagCtxt;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
use rustc_infer::infer;
use rustc_infer::infer::error_reporting::sub_relations::SubRelations;
use rustc_infer::infer::error_reporting::TypeErrCtxt;
Expand Down Expand Up @@ -222,15 +222,10 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
self.body_id
}

fn re_infer(
&self,
def: Option<&ty::GenericParamDef>,
span: Span,
_object_lifetime_default: bool,
) -> ty::Region<'tcx> {
let v = match def {
Some(def) => infer::RegionParameterDefinition(span, def.name),
None => infer::MiscVariable(span),
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
let v = match reason {
RegionInferReason::Param(def) => infer::RegionParameterDefinition(span, def.name),
_ => infer::MiscVariable(span),
};
self.next_region_var(v)
}
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use rustc_hir::GenericArg;
use rustc_hir_analysis::hir_ty_lowering::generics::{
check_generic_arg_count_for_call, lower_generic_args,
};
use rustc_hir_analysis::hir_ty_lowering::{GenericArgsLowerer, HirTyLowerer, IsMethodCall};
use rustc_hir_analysis::hir_ty_lowering::{
GenericArgsLowerer, HirTyLowerer, IsMethodCall, RegionInferReason,
};
use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk};
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
Expand Down Expand Up @@ -388,9 +390,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
arg: &GenericArg<'tcx>,
) -> ty::GenericArg<'tcx> {
match (&param.kind, arg) {
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
self.cfcx.fcx.lowerer().lower_lifetime(lt, Some(param)).into()
}
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => self
.cfcx
.fcx
.lowerer()
.lower_lifetime(lt, RegionInferReason::Param(param))
.into(),
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
self.cfcx.lower_ty(ty).raw.into()
}
Expand Down

0 comments on commit ac117f0

Please sign in to comment.