try to avoid FnCtxt during wf

This commit is contained in:
lcnr 2022-07-07 15:12:32 +02:00
parent 29d0390b97
commit 4b56fd9341
15 changed files with 471 additions and 448 deletions

View File

@ -1,7 +1,17 @@
use rustc_middle::ty::TyCtxt;
use std::cell::RefCell;
use super::TraitEngine;
use super::{ChalkFulfillmentContext, FulfillmentContext};
use crate::infer::InferCtxtExt;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::{InferCtxt, InferOk};
use rustc_infer::traits::{
FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
};
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::ToPredicate;
use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{self, Ty, TyCtxt};
pub trait TraitEngineExt<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
@ -16,3 +26,87 @@ impl<'tcx> TraitEngineExt<'tcx> for dyn TraitEngine<'tcx> {
}
}
}
/// Used if you want to have pleasant experience when dealing
/// with obligations outside of hir or mir typeck.
pub struct ObligationCtxt<'a, 'tcx> {
pub infcx: &'a InferCtxt<'a, 'tcx>,
engine: RefCell<Box<dyn TraitEngine<'tcx>>>,
}
impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)) }
}
pub fn register_obligation(&self, obligation: PredicateObligation<'tcx>) {
self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation);
}
pub fn register_obligations(
&self,
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
) {
// Can't use `register_predicate_obligations` because the iterator
// may also use this `ObligationCtxt`.
for obligation in obligations {
self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation)
}
}
pub fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
let InferOk { value, obligations } = infer_ok;
self.engine.borrow_mut().register_predicate_obligations(self.infcx, obligations);
value
}
/// Requires that `ty` must implement the trait with `def_id` in
/// the given environment. This trait must not have any type
/// parameters (except for `Self`).
pub fn register_bound(
&self,
cause: ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
ty: Ty<'tcx>,
def_id: DefId,
) {
let tcx = self.infcx.tcx;
let trait_ref = ty::TraitRef { def_id, substs: tcx.mk_substs_trait(ty, &[]) };
self.register_obligation(Obligation {
cause,
recursion_depth: 0,
param_env,
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
});
}
pub fn normalize<T: TypeFoldable<'tcx>>(
&self,
cause: ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
value: T,
) -> T {
let infer_ok = self.infcx.partially_normalize_associated_types_in(cause, param_env, value);
self.register_infer_ok_obligations(infer_ok)
}
pub fn equate_types(
&self,
cause: &ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
expected: Ty<'tcx>,
actual: Ty<'tcx>,
) -> Result<(), TypeError<'tcx>> {
match self.infcx.at(cause, param_env).eq(expected, actual) {
Ok(InferOk { obligations, value: () }) => {
self.register_obligations(obligations);
Ok(())
}
Err(e) => Err(e),
}
}
pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> {
self.engine.borrow_mut().select_all_or_error(self.infcx)
}
}

View File

@ -47,7 +47,7 @@ pub use self::SelectionError::*;
pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
pub use self::coherence::{OrphanCheckErr, OverlapResult};
pub use self::engine::TraitEngineExt;
pub use self::engine::{ObligationCtxt, TraitEngineExt};
pub use self::fulfill::{FulfillmentContext, PendingPredicateObligation};
pub use self::object_safety::astconv_object_safety_violations;
pub use self::object_safety::is_vtable_safe_method;

View File

@ -1,10 +1,7 @@
use crate::check::wfcheck::for_item;
use super::coercion::CoerceMany;
use super::compare_method::check_type_bounds;
use super::compare_method::{compare_const_impl, compare_impl_method, compare_ty_impl};
use super::*;
use rustc_attr as attr;
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
@ -29,8 +26,8 @@ use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVE
use rustc_span::symbol::sym;
use rustc_span::{self, Span};
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
use rustc_trait_selection::traits::{self, ObligationCtxt};
use rustc_ty_utils::representability::{self, Representability};
use std::iter;
@ -733,14 +730,13 @@ fn check_opaque_meets_bounds<'tcx>(
let param_env = tcx.param_env(defining_use_anchor);
tcx.infer_ctxt().with_opaque_type_inference(defining_use_anchor).enter(move |infcx| {
let inh = Inherited::new(infcx, def_id);
let infcx = &inh.infcx;
let ocx = ObligationCtxt::new(&infcx);
let opaque_ty = tcx.mk_opaque(def_id.to_def_id(), substs);
let misc_cause = traits::ObligationCause::misc(span, hir_id);
match infcx.at(&misc_cause, param_env).eq(opaque_ty, hidden_type) {
Ok(infer_ok) => inh.register_infer_ok_obligations(infer_ok),
Ok(infer_ok) => ocx.register_infer_ok_obligations(infer_ok),
Err(ty_err) => {
tcx.sess.delay_span_bug(
span,
@ -754,11 +750,11 @@ fn check_opaque_meets_bounds<'tcx>(
// hidden type is well formed even without those bounds.
let predicate =
ty::Binder::dummy(ty::PredicateKind::WellFormed(hidden_type.into())).to_predicate(tcx);
inh.register_predicate(Obligation::new(misc_cause, param_env, predicate));
ocx.register_obligation(Obligation::new(misc_cause, param_env, predicate));
// Check that all obligations are satisfied by the implementation's
// version.
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
infcx.report_fulfillment_errors(&errors, None, false);
}
@ -940,9 +936,10 @@ fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
DefKind::GlobalAsm => {
let it = tcx.hir().item(id);
let hir::ItemKind::GlobalAsm(asm) = it.kind else { span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it) };
for_item(tcx, it).with_fcx(|fcx| {
Inherited::build(tcx, it.def_id).enter(|inh| {
let fcx = FnCtxt::new(&inh, tcx.param_env(it.def_id), id.hir_id());
fcx.check_asm(asm, it.hir_id());
Default::default()
fcx.select_all_obligations_or_error();
})
}
_ => {}

View File

@ -1,4 +1,4 @@
use super::{potentially_plural_count, Inherited};
use super::potentially_plural_count;
use crate::check::regionck::OutlivesEnvironmentExt;
use crate::check::wfcheck;
use crate::errors::LifetimesOrBoundsMismatchOnTrait;
@ -9,7 +9,7 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit;
use rustc_hir::{GenericParamKind, ImplItemKind, TraitItemKind};
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{self, InferOk, TyCtxtInferExt};
use rustc_infer::infer::{self, TyCtxtInferExt};
use rustc_infer::traits::util;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::subst::{InternalSubsts, Subst};
@ -18,7 +18,9 @@ use rustc_middle::ty::{self, DefIdTree};
use rustc_middle::ty::{GenericParamDefKind, ToPredicate, TyCtxt};
use rustc_span::Span;
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, Reveal};
use rustc_trait_selection::traits::{
self, ObligationCause, ObligationCauseCode, ObligationCtxt, Reveal,
};
use std::iter;
/// Checks that a method from an impl conforms to the signature of
@ -205,21 +207,19 @@ fn compare_predicate_entailment<'tcx>(
);
let param_env = traits::normalize_param_env_or_error(tcx, param_env, normalize_cause);
tcx.infer_ctxt().enter(|infcx| {
let inh = Inherited::new(infcx, impl_m.def_id.expect_local());
let infcx = &inh.infcx;
tcx.infer_ctxt().enter(|ref infcx| {
let ocx = ObligationCtxt::new(infcx);
debug!("compare_impl_method: caller_bounds={:?}", param_env.caller_bounds());
let mut selcx = traits::SelectionContext::new(&infcx);
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs);
for (predicate, span) in iter::zip(impl_m_own_bounds.predicates, impl_m_own_bounds.spans) {
let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id);
let traits::Normalized { value: predicate, obligations } =
traits::normalize(&mut selcx, param_env, normalize_cause, predicate);
inh.register_predicates(obligations);
ocx.register_obligations(obligations);
let cause = ObligationCause::new(
span,
impl_m_hir_id,
@ -228,7 +228,7 @@ fn compare_predicate_entailment<'tcx>(
trait_item_def_id: trait_m.def_id,
},
);
inh.register_predicate(traits::Obligation::new(cause, param_env, predicate));
ocx.register_obligation(traits::Obligation::new(cause, param_env, predicate));
}
// We now need to check that the signature of the impl method is
@ -254,32 +254,31 @@ fn compare_predicate_entailment<'tcx>(
infer::HigherRankedType,
tcx.fn_sig(impl_m.def_id),
);
let impl_sig =
inh.normalize_associated_types_in(impl_m_span, impl_m_hir_id, param_env, impl_sig);
let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id);
let impl_sig = ocx.normalize(norm_cause.clone(), param_env, impl_sig);
let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig));
debug!("compare_impl_method: impl_fty={:?}", impl_fty);
let trait_sig = tcx.bound_fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs);
let trait_sig = tcx.liberate_late_bound_regions(impl_m.def_id, trait_sig);
let trait_sig =
inh.normalize_associated_types_in(impl_m_span, impl_m_hir_id, param_env, trait_sig);
let trait_sig = ocx.normalize(norm_cause, param_env, trait_sig);
// Add the resulting inputs and output as well-formed.
wf_tys.extend(trait_sig.inputs_and_output.iter());
let trait_fty = tcx.mk_fn_ptr(ty::Binder::dummy(trait_sig));
debug!("compare_impl_method: trait_fty={:?}", trait_fty);
let sub_result = infcx.at(&cause, param_env).sup(trait_fty, impl_fty).map(
|InferOk { obligations, .. }| {
// FIXME: We'd want to keep more accurate spans than "the method signature" when
// processing the comparison between the trait and impl fn, but we sadly lose them
// and point at the whole signature when a trait bound or specific input or output
// type would be more appropriate. In other places we have a `Vec<Span>`
// corresponding to their `Vec<Predicate>`, but we don't have that here.
// Fixing this would improve the output of test `issue-83765.rs`.
inh.register_predicates(obligations);
},
);
// FIXME: We'd want to keep more accurate spans than "the method signature" when
// processing the comparison between the trait and impl fn, but we sadly lose them
// and point at the whole signature when a trait bound or specific input or output
// type would be more appropriate. In other places we have a `Vec<Span>`
// corresponding to their `Vec<Predicate>`, but we don't have that here.
// Fixing this would improve the output of test `issue-83765.rs`.
let sub_result = infcx
.at(&cause, param_env)
.sup(trait_fty, impl_fty)
.map(|infer_ok| ocx.register_infer_ok_obligations(infer_ok));
if let Err(terr) = sub_result {
debug!("sub_types failed: impl ty {:?}, trait ty {:?}", impl_fty, trait_fty);
@ -385,7 +384,7 @@ fn compare_predicate_entailment<'tcx>(
// Check that all obligations are satisfied by the implementation's
// version.
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
let reported = infcx.report_fulfillment_errors(&errors, None, false);
return Err(reported);
@ -1058,8 +1057,7 @@ pub(crate) fn compare_const_impl<'tcx>(
tcx.infer_ctxt().enter(|infcx| {
let param_env = tcx.param_env(impl_c.def_id);
let inh = Inherited::new(infcx, impl_c.def_id.expect_local());
let infcx = &inh.infcx;
let ocx = ObligationCtxt::new(&infcx);
// The below is for the most part highly similar to the procedure
// for methods above. It is simpler in many respects, especially
@ -1082,20 +1080,18 @@ pub(crate) fn compare_const_impl<'tcx>(
);
// There is no "body" here, so just pass dummy id.
let impl_ty =
inh.normalize_associated_types_in(impl_c_span, impl_c_hir_id, param_env, impl_ty);
let impl_ty = ocx.normalize(cause.clone(), param_env, impl_ty);
debug!("compare_const_impl: impl_ty={:?}", impl_ty);
let trait_ty =
inh.normalize_associated_types_in(impl_c_span, impl_c_hir_id, param_env, trait_ty);
let trait_ty = ocx.normalize(cause.clone(), param_env, trait_ty);
debug!("compare_const_impl: trait_ty={:?}", trait_ty);
let err = infcx
.at(&cause, param_env)
.sup(trait_ty, impl_ty)
.map(|ok| inh.register_infer_ok_obligations(ok));
.map(|ok| ocx.register_infer_ok_obligations(ok));
if let Err(terr) = err {
debug!(
@ -1142,7 +1138,7 @@ pub(crate) fn compare_const_impl<'tcx>(
// Check that all obligations are satisfied by the implementation's
// version.
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
infcx.report_fulfillment_errors(&errors, None, false);
return;
@ -1241,8 +1237,7 @@ fn compare_type_predicate_entailment<'tcx>(
);
let param_env = traits::normalize_param_env_or_error(tcx, param_env, normalize_cause.clone());
tcx.infer_ctxt().enter(|infcx| {
let inh = Inherited::new(infcx, impl_ty.def_id.expect_local());
let infcx = &inh.infcx;
let ocx = ObligationCtxt::new(&infcx);
debug!("compare_type_predicate_entailment: caller_bounds={:?}", param_env.caller_bounds());
@ -1252,13 +1247,13 @@ fn compare_type_predicate_entailment<'tcx>(
let traits::Normalized { value: predicate, obligations } =
traits::normalize(&mut selcx, param_env, normalize_cause.clone(), predicate);
inh.register_predicates(obligations);
inh.register_predicate(traits::Obligation::new(cause.clone(), param_env, predicate));
ocx.register_obligations(obligations);
ocx.register_obligation(traits::Obligation::new(cause.clone(), param_env, predicate));
}
// Check that all obligations are satisfied by the implementation's
// version.
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
let reported = infcx.report_fulfillment_errors(&errors, None, false);
return Err(reported);
@ -1431,10 +1426,9 @@ pub fn check_type_bounds<'tcx>(
impl_ty_substs.rebase_onto(tcx, impl_ty.container.id(), impl_trait_ref.substs);
tcx.infer_ctxt().enter(move |infcx| {
let inh = Inherited::new(infcx, impl_ty.def_id.expect_local());
let infcx = &inh.infcx;
let mut selcx = traits::SelectionContext::new(&infcx);
let ocx = ObligationCtxt::new(&infcx);
let mut selcx = traits::SelectionContext::new(&infcx);
let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local());
let normalize_cause = ObligationCause::new(
impl_ty_span,
@ -1477,13 +1471,13 @@ pub fn check_type_bounds<'tcx>(
debug!("compare_projection_bounds: normalized predicate = {:?}", normalized_predicate);
obligation.predicate = normalized_predicate;
inh.register_predicates(obligations);
inh.register_predicate(obligation);
ocx.register_obligations(obligations);
ocx.register_obligation(obligation);
}
// Check that all obligations are satisfied by the implementation's
// version.
let errors = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
let reported = infcx.report_fulfillment_errors(&errors, None, false);
return Err(reported);
@ -1498,7 +1492,7 @@ pub fn check_type_bounds<'tcx>(
}
};
let mut outlives_environment = OutlivesEnvironment::new(param_env);
outlives_environment.add_implied_bounds(infcx, implied_bounds, impl_ty_hir_id);
outlives_environment.add_implied_bounds(&infcx, implied_bounds, impl_ty_hir_id);
infcx.check_region_obligations_and_report_errors(&outlives_environment);
Ok(())

View File

@ -37,7 +37,6 @@ use rustc_trait_selection::infer::InferCtxtExt as _;
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
use rustc_trait_selection::traits::{
self, ObligationCause, ObligationCauseCode, StatementAsExpression, TraitEngine, TraitEngineExt,
WellFormedLoc,
};
use std::collections::hash_map::Entry;
@ -375,29 +374,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(result, spans)
}
/// Convenience method which tracks extra diagnostic information for normalization
/// that occurs as a result of WF checking. The `hir_id` is the `HirId` of the hir item
/// whose type is being wf-checked - this is used to construct a more precise span if
/// an error occurs.
///
/// It is never necessary to call this method - calling `normalize_associated_types_in` will
/// just result in a slightly worse diagnostic span, and will still be sound.
pub(in super::super) fn normalize_associated_types_in_wf<T>(
&self,
span: Span,
value: T,
loc: WellFormedLoc,
) -> T
where
T: TypeFoldable<'tcx>,
{
self.inh.normalize_associated_types_in_with_cause(
ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(Some(loc))),
self.param_env,
value,
)
}
pub(in super::super) fn normalize_associated_types_in<T>(&self, span: Span, value: T) -> T
where
T: TypeFoldable<'tcx>,

View File

@ -1,5 +1,4 @@
use crate::check::regionck::OutlivesEnvironmentExt;
use crate::check::{FnCtxt, Inherited};
use crate::constrained_generic_params::{identify_constrained_generic_params, Parameter};
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@ -23,55 +22,95 @@ use rustc_middle::ty::{
use rustc_session::parse::feature_err;
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_trait_selection::autoderef::Autoderef;
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_trait_selection::traits::query::normalize::AtExt;
use rustc_trait_selection::traits::query::NoSolution;
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, WellFormedLoc};
use rustc_trait_selection::traits::{
self, ObligationCause, ObligationCauseCode, ObligationCtxt, WellFormedLoc,
};
use std::cell::LazyCell;
use std::convert::TryInto;
use std::iter;
use std::ops::ControlFlow;
use std::ops::{ControlFlow, Deref};
/// Helper type of a temporary returned by `.for_item(...)`.
/// This is necessary because we can't write the following bound:
///
/// ```ignore (illustrative)
/// F: for<'b, 'tcx> where 'tcx FnOnce(FnCtxt<'b, 'tcx>)
/// ```
pub(super) struct CheckWfFcxBuilder<'tcx> {
inherited: super::InheritedBuilder<'tcx>,
id: hir::HirId,
pub(super) struct WfCheckingCtxt<'a, 'tcx> {
pub(super) ocx: ObligationCtxt<'a, 'tcx>,
span: Span,
body_id: hir::HirId,
param_env: ty::ParamEnv<'tcx>,
}
impl<'tcx> CheckWfFcxBuilder<'tcx> {
pub(super) fn with_fcx<F>(&mut self, f: F)
where
F: for<'b> FnOnce(&FnCtxt<'b, 'tcx>) -> FxHashSet<Ty<'tcx>>,
{
let id = self.id;
let span = self.span;
let param_env = self.param_env;
self.inherited.enter(|inh| {
let fcx = FnCtxt::new(&inh, param_env, id);
if !inh.tcx.features().trivial_bounds {
// As predicates are cached rather than obligations, this
// needs to be called first so that they are checked with an
// empty `param_env`.
check_false_global_bounds(&fcx, span, id);
}
let wf_tys = f(&fcx);
fcx.select_all_obligations_or_error();
let mut outlives_environment = OutlivesEnvironment::new(param_env);
outlives_environment.add_implied_bounds(&fcx.infcx, wf_tys, id);
fcx.infcx.check_region_obligations_and_report_errors(&outlives_environment);
});
impl<'a, 'tcx> Deref for WfCheckingCtxt<'a, 'tcx> {
type Target = ObligationCtxt<'a, 'tcx>;
fn deref(&self) -> &Self::Target {
&self.ocx
}
}
impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.ocx.infcx.tcx
}
fn normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
where
T: TypeFoldable<'tcx>,
{
self.ocx.normalize(
ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)),
self.param_env,
value,
)
}
fn register_wf_obligation(
&self,
span: Span,
loc: Option<WellFormedLoc>,
arg: ty::GenericArg<'tcx>,
) {
let cause =
traits::ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc));
self.ocx.register_obligation(traits::Obligation::new(
cause,
self.param_env,
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)).to_predicate(self.tcx()),
));
}
}
pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
tcx: TyCtxt<'tcx>,
span: Span,
body_def_id: LocalDefId,
f: F,
) where
F: for<'a> FnOnce(&WfCheckingCtxt<'a, 'tcx>) -> FxHashSet<Ty<'tcx>>,
{
let param_env = tcx.param_env(body_def_id);
let body_id = tcx.hir().local_def_id_to_hir_id(body_def_id);
tcx.infer_ctxt().enter(|ref infcx| {
let ocx = ObligationCtxt::new(infcx);
let mut wfcx = WfCheckingCtxt { ocx, span, body_id, param_env };
if !tcx.features().trivial_bounds {
wfcx.check_false_global_bounds()
}
let wf_tys = f(&mut wfcx);
let errors = wfcx.select_all_or_error();
if !errors.is_empty() {
infcx.report_fulfillment_errors(&errors, None, false);
return;
}
let mut outlives_environment = OutlivesEnvironment::new(param_env);
outlives_environment.add_implied_bounds(infcx, wf_tys, body_id);
infcx.check_region_obligations_and_report_errors(&outlives_environment);
})
}
fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let node = tcx.hir().expect_owner(def_id);
match node {
@ -176,17 +215,17 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
check_item_type(tcx, item.def_id, ty.span, false);
}
hir::ItemKind::Struct(ref struct_def, ref ast_generics) => {
check_type_defn(tcx, item, false, |fcx| vec![fcx.non_enum_variant(struct_def)]);
check_type_defn(tcx, item, false, |wfcx| vec![wfcx.non_enum_variant(struct_def)]);
check_variances_for_type_defn(tcx, item, ast_generics);
}
hir::ItemKind::Union(ref struct_def, ref ast_generics) => {
check_type_defn(tcx, item, true, |fcx| vec![fcx.non_enum_variant(struct_def)]);
check_type_defn(tcx, item, true, |wfcx| vec![wfcx.non_enum_variant(struct_def)]);
check_variances_for_type_defn(tcx, item, ast_generics);
}
hir::ItemKind::Enum(ref enum_def, ref ast_generics) => {
check_type_defn(tcx, item, true, |fcx| fcx.enum_variants(enum_def));
check_type_defn(tcx, item, true, |wfcx| wfcx.enum_variants(enum_def));
check_variances_for_type_defn(tcx, item, ast_generics);
}
@ -935,46 +974,45 @@ fn check_associated_item(
span: Span,
sig_if_method: Option<&hir::FnSig<'_>>,
) {
let code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(item_id)));
for_id(tcx, item_id, span).with_fcx(|fcx| {
let item = fcx.tcx.associated_item(item_id);
let loc = Some(WellFormedLoc::Ty(item_id));
enter_wf_checking_ctxt(tcx, span, item_id, |wfcx| {
let item = tcx.associated_item(item_id);
let (mut implied_bounds, self_ty) = match item.container {
ty::TraitContainer(_) => (FxHashSet::default(), fcx.tcx.types.self_param),
ty::TraitContainer(_) => (FxHashSet::default(), tcx.types.self_param),
ty::ImplContainer(def_id) => (
impl_implied_bounds(tcx, fcx.param_env, def_id.expect_local(), span),
fcx.tcx.type_of(def_id),
impl_implied_bounds(tcx, wfcx.param_env, def_id.expect_local(), span),
tcx.type_of(def_id),
),
};
match item.kind {
ty::AssocKind::Const => {
let ty = fcx.tcx.type_of(item.def_id);
let ty = fcx.normalize_associated_types_in_wf(span, ty, WellFormedLoc::Ty(item_id));
fcx.register_wf_obligation(ty.into(), span, code.clone());
let ty = tcx.type_of(item.def_id);
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
wfcx.register_wf_obligation(span, loc, ty.into());
}
ty::AssocKind::Fn => {
let sig = fcx.tcx.fn_sig(item.def_id);
let sig = tcx.fn_sig(item.def_id);
let hir_sig = sig_if_method.expect("bad signature for method");
check_fn_or_method(
fcx,
item.ident(fcx.tcx).span,
wfcx,
item.ident(tcx).span,
sig,
hir_sig.decl,
item.def_id.expect_local(),
&mut implied_bounds,
);
check_method_receiver(fcx, hir_sig, item, self_ty);
check_method_receiver(wfcx, hir_sig, item, self_ty);
}
ty::AssocKind::Type => {
if let ty::AssocItemContainer::TraitContainer(_) = item.container {
check_associated_type_bounds(fcx, item, span)
check_associated_type_bounds(wfcx, item, span)
}
if item.defaultness.has_value() {
let ty = fcx.tcx.type_of(item.def_id);
let ty =
fcx.normalize_associated_types_in_wf(span, ty, WellFormedLoc::Ty(item_id));
fcx.register_wf_obligation(ty.into(), span, code.clone());
let ty = tcx.type_of(item.def_id);
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
wfcx.register_wf_obligation(span, loc, ty.into());
}
}
}
@ -983,19 +1021,6 @@ fn check_associated_item(
})
}
pub(super) fn for_item<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>) -> CheckWfFcxBuilder<'tcx> {
for_id(tcx, item.def_id, item.span)
}
fn for_id(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) -> CheckWfFcxBuilder<'_> {
CheckWfFcxBuilder {
inherited: Inherited::build(tcx, def_id),
id: hir::HirId::make_owner(def_id),
span,
param_env: tcx.param_env(def_id),
}
}
fn item_adt_kind(kind: &ItemKind<'_>) -> Option<AdtKind> {
match kind {
ItemKind::Struct(..) => Some(AdtKind::Struct),
@ -1012,19 +1037,19 @@ fn check_type_defn<'tcx, F>(
all_sized: bool,
mut lookup_fields: F,
) where
F: for<'fcx> FnMut(&FnCtxt<'fcx, 'tcx>) -> Vec<AdtVariant<'tcx>>,
F: FnMut(&WfCheckingCtxt<'_, 'tcx>) -> Vec<AdtVariant<'tcx>>,
{
for_item(tcx, item).with_fcx(|fcx| {
let variants = lookup_fields(fcx);
enter_wf_checking_ctxt(tcx, item.span, item.def_id, |wfcx| {
let variants = lookup_fields(wfcx);
let packed = tcx.adt_def(item.def_id).repr().packed();
for variant in &variants {
// All field types must be well-formed.
for field in &variant.fields {
fcx.register_wf_obligation(
field.ty.into(),
wfcx.register_wf_obligation(
field.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
Some(WellFormedLoc::Ty(field.def_id)),
field.ty.into(),
)
}
@ -1051,12 +1076,10 @@ fn check_type_defn<'tcx, F>(
variant.fields[..variant.fields.len() - unsized_len].iter().enumerate()
{
let last = idx == variant.fields.len() - 1;
fcx.register_bound(
field.ty,
tcx.require_lang_item(LangItem::Sized, None),
wfcx.register_bound(
traits::ObligationCause::new(
field.span,
fcx.body_id,
wfcx.body_id,
traits::FieldSized {
adt_kind: match item_adt_kind(&item.kind) {
Some(i) => i,
@ -1066,6 +1089,9 @@ fn check_type_defn<'tcx, F>(
last,
},
),
wfcx.param_env,
field.ty,
tcx.require_lang_item(LangItem::Sized, None),
);
}
@ -1075,12 +1101,12 @@ fn check_type_defn<'tcx, F>(
let cause = traits::ObligationCause::new(
tcx.def_span(discr_def_id),
fcx.body_id,
wfcx.body_id,
traits::MiscObligation,
);
fcx.register_predicate(traits::Obligation::new(
wfcx.register_obligation(traits::Obligation::new(
cause,
fcx.param_env,
wfcx.param_env,
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ty::Unevaluated::new(
ty::WithOptConstParam::unknown(discr_def_id.to_def_id()),
discr_substs,
@ -1090,7 +1116,7 @@ fn check_type_defn<'tcx, F>(
}
}
check_where_clauses(fcx, item.span, item.def_id);
check_where_clauses(wfcx, item.span, item.def_id);
// No implied bounds in a struct definition.
FxHashSet::default()
@ -1116,9 +1142,8 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
}
}
// FIXME: this shouldn't use an `FnCtxt` at all.
for_item(tcx, item).with_fcx(|fcx| {
check_where_clauses(fcx, item.span, item.def_id);
enter_wf_checking_ctxt(tcx, item.span, item.def_id, |wfcx| {
check_where_clauses(wfcx, item.span, item.def_id);
FxHashSet::default()
});
@ -1133,27 +1158,22 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
///
/// Assuming the defaults are used, check that all predicates (bounds on the
/// assoc type and where clauses on the trait) hold.
fn check_associated_type_bounds(fcx: &FnCtxt<'_, '_>, item: &ty::AssocItem, span: Span) {
let tcx = fcx.tcx;
let bounds = tcx.explicit_item_bounds(item.def_id);
fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: &ty::AssocItem, span: Span) {
let bounds = wfcx.tcx().explicit_item_bounds(item.def_id);
debug!("check_associated_type_bounds: bounds={:?}", bounds);
let wf_obligations = bounds.iter().flat_map(|&(bound, bound_span)| {
let normalized_bound = fcx.normalize_associated_types_in(span, bound);
let normalized_bound = wfcx.normalize(span, None, bound);
traits::wf::predicate_obligations(
fcx,
fcx.param_env,
fcx.body_id,
wfcx.infcx,
wfcx.param_env,
wfcx.body_id,
normalized_bound,
bound_span,
)
});
for obligation in wf_obligations {
debug!("next obligation cause: {:?}", obligation.cause);
fcx.register_predicate(obligation);
}
wfcx.register_obligations(wf_obligations);
}
fn check_item_fn(
@ -1163,10 +1183,10 @@ fn check_item_fn(
span: Span,
decl: &hir::FnDecl<'_>,
) {
for_id(tcx, def_id, span).with_fcx(|fcx| {
enter_wf_checking_ctxt(tcx, span, def_id, |wfcx| {
let sig = tcx.fn_sig(def_id);
let mut implied_bounds = FxHashSet::default();
check_fn_or_method(fcx, ident.span, sig, decl, def_id, &mut implied_bounds);
check_fn_or_method(wfcx, ident.span, sig, decl, def_id, &mut implied_bounds);
implied_bounds
})
}
@ -1174,28 +1194,25 @@ fn check_item_fn(
fn check_item_type(tcx: TyCtxt<'_>, item_id: LocalDefId, ty_span: Span, allow_foreign_ty: bool) {
debug!("check_item_type: {:?}", item_id);
for_id(tcx, item_id, ty_span).with_fcx(|fcx| {
enter_wf_checking_ctxt(tcx, ty_span, item_id, |wfcx| {
let ty = tcx.type_of(item_id);
let item_ty = fcx.normalize_associated_types_in_wf(ty_span, ty, WellFormedLoc::Ty(item_id));
let item_ty = wfcx.normalize(ty_span, Some(WellFormedLoc::Ty(item_id)), ty);
let mut forbid_unsized = true;
if allow_foreign_ty {
let tail = fcx.tcx.struct_tail_erasing_lifetimes(item_ty, fcx.param_env);
let tail = tcx.struct_tail_erasing_lifetimes(item_ty, wfcx.param_env);
if let ty::Foreign(_) = tail.kind() {
forbid_unsized = false;
}
}
fcx.register_wf_obligation(
item_ty.into(),
ty_span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(item_id))),
);
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
if forbid_unsized {
fcx.register_bound(
wfcx.register_bound(
traits::ObligationCause::new(ty_span, wfcx.body_id, traits::WellFormed(None)),
wfcx.param_env,
item_ty,
tcx.require_lang_item(LangItem::Sized, None),
traits::ObligationCause::new(ty_span, fcx.body_id, traits::WellFormed(None)),
);
}
@ -1206,10 +1223,11 @@ fn check_item_type(tcx: TyCtxt<'_>, item_id: LocalDefId, ty_span: Span, allow_fo
&& !tcx.is_thread_local_static(item_id.to_def_id());
if should_check_for_sync {
fcx.register_bound(
wfcx.register_bound(
traits::ObligationCause::new(ty_span, wfcx.body_id, traits::SharedStatic),
wfcx.param_env,
item_ty,
tcx.require_lang_item(LangItem::Sync, Some(ty_span)),
traits::ObligationCause::new(ty_span, fcx.body_id, traits::SharedStatic),
);
}
@ -1225,55 +1243,47 @@ fn check_impl<'tcx>(
ast_self_ty: &hir::Ty<'_>,
ast_trait_ref: &Option<hir::TraitRef<'_>>,
) {
for_item(tcx, item).with_fcx(|fcx| {
enter_wf_checking_ctxt(tcx, item.span, item.def_id, |wfcx| {
match *ast_trait_ref {
Some(ref ast_trait_ref) => {
// `#[rustc_reservation_impl]` impls are not real impls and
// therefore don't need to be WF (the trait's `Self: Trait` predicate
// won't hold).
let trait_ref = tcx.impl_trait_ref(item.def_id).unwrap();
let trait_ref =
fcx.normalize_associated_types_in(ast_trait_ref.path.span, trait_ref);
let trait_ref = wfcx.normalize(ast_trait_ref.path.span, None, trait_ref);
let obligations = traits::wf::trait_obligations(
fcx,
fcx.param_env,
fcx.body_id,
wfcx.infcx,
wfcx.param_env,
wfcx.body_id,
&trait_ref,
ast_trait_ref.path.span,
item,
);
debug!(?obligations);
for obligation in obligations {
fcx.register_predicate(obligation);
}
wfcx.register_obligations(obligations);
}
None => {
let self_ty = tcx.type_of(item.def_id);
let self_ty = fcx.normalize_associated_types_in(item.span, self_ty);
fcx.register_wf_obligation(
self_ty.into(),
let self_ty = wfcx.normalize(item.span, None, self_ty);
wfcx.register_wf_obligation(
ast_self_ty.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(
item.hir_id().expect_owner(),
))),
Some(WellFormedLoc::Ty(item.hir_id().expect_owner())),
self_ty.into(),
);
}
}
check_where_clauses(fcx, item.span, item.def_id);
check_where_clauses(wfcx, item.span, item.def_id);
impl_implied_bounds(tcx, fcx.param_env, item.def_id, item.span)
impl_implied_bounds(tcx, wfcx.param_env, item.def_id, item.span)
});
}
/// Checks where-clauses and inline bounds that are declared on `def_id`.
#[instrument(skip(fcx), level = "debug")]
fn check_where_clauses<'tcx, 'fcx>(
fcx: &FnCtxt<'fcx, 'tcx>,
span: Span,
def_id: LocalDefId,
) {
let tcx = fcx.tcx;
#[instrument(level = "debug", skip(wfcx))]
fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id: LocalDefId) {
let infcx = wfcx.infcx;
let tcx = wfcx.tcx();
let predicates = tcx.predicates_of(def_id);
let generics = tcx.generics_of(def_id);
@ -1301,11 +1311,7 @@ fn check_where_clauses<'tcx, 'fcx>(
// parameter includes another (e.g., `<T, U = T>`). In those cases, we can't
// be sure if it will error or not as user might always specify the other.
if !ty.needs_subst() {
fcx.register_wf_obligation(
ty.into(),
tcx.def_span(param.def_id),
ObligationCauseCode::MiscObligation,
);
wfcx.register_wf_obligation(tcx.def_span(param.def_id), None, ty.into());
}
}
}
@ -1317,10 +1323,10 @@ fn check_where_clauses<'tcx, 'fcx>(
// we should eagerly error.
let default_ct = tcx.const_param_default(param.def_id);
if !default_ct.needs_subst() {
fcx.register_wf_obligation(
default_ct.into(),
wfcx.register_wf_obligation(
tcx.def_span(param.def_id),
ObligationCauseCode::WellFormed(None),
None,
default_ct.into(),
);
}
}
@ -1431,42 +1437,41 @@ fn check_where_clauses<'tcx, 'fcx>(
// Note the subtle difference from how we handle `predicates`
// below: there, we are not trying to prove those predicates
// to be *true* but merely *well-formed*.
let pred = fcx.normalize_associated_types_in(sp, pred);
let pred = wfcx.normalize(sp, None, pred);
let cause = traits::ObligationCause::new(
sp,
fcx.body_id,
wfcx.body_id,
traits::ItemObligation(def_id.to_def_id()),
);
traits::Obligation::new(cause, fcx.param_env, pred)
traits::Obligation::new(cause, wfcx.param_env, pred)
});
let predicates = predicates.instantiate_identity(tcx);
let predicates = fcx.normalize_associated_types_in(span, predicates);
let predicates = wfcx.normalize(span, None, predicates);
debug!(?predicates.predicates);
assert_eq!(predicates.predicates.len(), predicates.spans.len());
let wf_obligations =
iter::zip(&predicates.predicates, &predicates.spans).flat_map(|(&p, &sp)| {
traits::wf::predicate_obligations(fcx, fcx.param_env, fcx.body_id, p, sp)
traits::wf::predicate_obligations(infcx, wfcx.param_env, wfcx.body_id, p, sp)
});
for obligation in wf_obligations.chain(default_obligations) {
debug!("next obligation cause: {:?}", obligation.cause);
fcx.register_predicate(obligation);
}
let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect();
wfcx.register_obligations(obligations);
}
#[tracing::instrument(level = "debug", skip(fcx, span, hir_decl))]
fn check_fn_or_method<'fcx, 'tcx>(
fcx: &FnCtxt<'fcx, 'tcx>,
#[tracing::instrument(level = "debug", skip(wfcx, span, hir_decl))]
fn check_fn_or_method<'tcx>(
wfcx: &WfCheckingCtxt<'_, 'tcx>,
span: Span,
sig: ty::PolyFnSig<'tcx>,
hir_decl: &hir::FnDecl<'_>,
def_id: LocalDefId,
implied_bounds: &mut FxHashSet<Ty<'tcx>>,
) {
let sig = fcx.tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
let tcx = wfcx.tcx();
let sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
// Normalize the input and output types one at a time, using a different
// `WellFormedLoc` for each. We cannot call `normalize_associated_types`
@ -1474,70 +1479,61 @@ fn check_fn_or_method<'fcx, 'tcx>(
// for each type, preventing the HIR wf check from generating
// a nice error message.
let ty::FnSig { mut inputs_and_output, c_variadic, unsafety, abi } = sig;
inputs_and_output =
fcx.tcx.mk_type_list(inputs_and_output.iter().enumerate().map(|(i, ty)| {
fcx.normalize_associated_types_in_wf(
span,
ty,
WellFormedLoc::Param {
function: def_id,
// Note that the `param_idx` of the output type is
// one greater than the index of the last input type.
param_idx: i.try_into().unwrap(),
},
)
}));
inputs_and_output = tcx.mk_type_list(inputs_and_output.iter().enumerate().map(|(i, ty)| {
wfcx.normalize(
span,
Some(WellFormedLoc::Param {
function: def_id,
// Note that the `param_idx` of the output type is
// one greater than the index of the last input type.
param_idx: i.try_into().unwrap(),
}),
ty,
)
}));
// Manually call `normalize_associated_types_in` on the other types
// in `FnSig`. This ensures that if the types of these fields
// ever change to include projections, we will start normalizing
// them automatically.
let sig = ty::FnSig {
inputs_and_output,
c_variadic: fcx.normalize_associated_types_in(span, c_variadic),
unsafety: fcx.normalize_associated_types_in(span, unsafety),
abi: fcx.normalize_associated_types_in(span, abi),
c_variadic: wfcx.normalize(span, None, c_variadic),
unsafety: wfcx.normalize(span, None, unsafety),
abi: wfcx.normalize(span, None, abi),
};
for (i, (&input_ty, ty)) in iter::zip(sig.inputs(), hir_decl.inputs).enumerate() {
fcx.register_wf_obligation(
input_ty.into(),
wfcx.register_wf_obligation(
ty.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
function: def_id,
param_idx: i.try_into().unwrap(),
})),
Some(WellFormedLoc::Param { function: def_id, param_idx: i.try_into().unwrap() }),
input_ty.into(),
);
}
implied_bounds.extend(sig.inputs());
fcx.register_wf_obligation(
sig.output().into(),
hir_decl.output.span(),
ObligationCauseCode::ReturnType,
);
wfcx.register_wf_obligation(hir_decl.output.span(), None, sig.output().into());
// FIXME(#27579) return types should not be implied bounds
implied_bounds.insert(sig.output());
debug!(?implied_bounds);
check_where_clauses(fcx, span, def_id);
check_where_clauses(wfcx, span, def_id);
}
const HELP_FOR_SELF_TYPE: &str = "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
`self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
of the previous types except `Self`)";
#[tracing::instrument(level = "debug", skip(fcx))]
fn check_method_receiver<'fcx, 'tcx>(
fcx: &FnCtxt<'fcx, 'tcx>,
#[tracing::instrument(level = "debug", skip(wfcx))]
fn check_method_receiver<'tcx>(
wfcx: &WfCheckingCtxt<'_, 'tcx>,
fn_sig: &hir::FnSig<'_>,
method: &ty::AssocItem,
self_ty: Ty<'tcx>,
) {
// Check that the method has a valid receiver type, given the type `Self`.
debug!("check_method_receiver({:?}, self_ty={:?})", method, self_ty);
let tcx = wfcx.tcx();
if !method.fn_has_self_parameter {
return;
@ -1545,28 +1541,28 @@ fn check_method_receiver<'fcx, 'tcx>(
let span = fn_sig.decl.inputs[0].span;
let sig = fcx.tcx.fn_sig(method.def_id);
let sig = fcx.tcx.liberate_late_bound_regions(method.def_id, sig);
let sig = fcx.normalize_associated_types_in(span, sig);
let sig = tcx.fn_sig(method.def_id);
let sig = tcx.liberate_late_bound_regions(method.def_id, sig);
let sig = wfcx.normalize(span, None, sig);
debug!("check_method_receiver: sig={:?}", sig);
let self_ty = fcx.normalize_associated_types_in(span, self_ty);
let self_ty = wfcx.normalize(span, None, self_ty);
let receiver_ty = sig.inputs()[0];
let receiver_ty = fcx.normalize_associated_types_in(span, receiver_ty);
let receiver_ty = wfcx.normalize(span, None, receiver_ty);
if fcx.tcx.features().arbitrary_self_types {
if !receiver_is_valid(fcx, span, receiver_ty, self_ty, true) {
if tcx.features().arbitrary_self_types {
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
// Report error; `arbitrary_self_types` was enabled.
e0307(fcx, span, receiver_ty);
e0307(tcx, span, receiver_ty);
}
} else {
if !receiver_is_valid(fcx, span, receiver_ty, self_ty, false) {
if receiver_is_valid(fcx, span, receiver_ty, self_ty, true) {
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, false) {
if receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
// Report error; would have worked with `arbitrary_self_types`.
feature_err(
&fcx.tcx.sess.parse_sess,
&tcx.sess.parse_sess,
sym::arbitrary_self_types,
span,
&format!(
@ -1578,15 +1574,15 @@ fn check_method_receiver<'fcx, 'tcx>(
.emit();
} else {
// Report error; would not have worked with `arbitrary_self_types`.
e0307(fcx, span, receiver_ty);
e0307(tcx, span, receiver_ty);
}
}
}
}
fn e0307<'tcx>(fcx: &FnCtxt<'_, 'tcx>, span: Span, receiver_ty: Ty<'_>) {
fn e0307<'tcx>(tcx: TyCtxt<'tcx>, span: Span, receiver_ty: Ty<'_>) {
struct_span_err!(
fcx.tcx.sess.diagnostic(),
tcx.sess.diagnostic(),
span,
E0307,
"invalid `self` parameter type: {receiver_ty}"
@ -1605,26 +1601,30 @@ fn e0307<'tcx>(fcx: &FnCtxt<'_, 'tcx>, span: Span, receiver_ty: Ty<'_>) {
/// N.B., there are cases this function returns `true` but causes an error to be emitted,
/// particularly when `receiver_ty` derefs to a type that is the same as `self_ty` but has the
/// wrong lifetime. Be careful of this if you are calling this function speculatively.
fn receiver_is_valid<'fcx, 'tcx>(
fcx: &FnCtxt<'fcx, 'tcx>,
fn receiver_is_valid<'tcx>(
wfcx: &WfCheckingCtxt<'_, 'tcx>,
span: Span,
receiver_ty: Ty<'tcx>,
self_ty: Ty<'tcx>,
arbitrary_self_types_enabled: bool,
) -> bool {
let cause = fcx.cause(span, traits::ObligationCauseCode::MethodReceiver);
let infcx = wfcx.infcx;
let tcx = wfcx.tcx();
let cause =
ObligationCause::new(span, wfcx.body_id, traits::ObligationCauseCode::MethodReceiver);
let can_eq_self = |ty| fcx.infcx.can_eq(fcx.param_env, self_ty, ty).is_ok();
let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty).is_ok();
// `self: Self` is always valid.
if can_eq_self(receiver_ty) {
if let Some(mut err) = fcx.demand_eqtype_with_origin(&cause, self_ty, receiver_ty) {
err.emit();
if let Err(err) = wfcx.equate_types(&cause, wfcx.param_env, self_ty, receiver_ty) {
infcx.report_mismatched_types(&cause, self_ty, receiver_ty, err).emit();
}
return true;
}
let mut autoderef = fcx.autoderef(span, receiver_ty);
let mut autoderef =
Autoderef::new(infcx, wfcx.param_env, wfcx.body_id, span, receiver_ty, span);
// The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`.
if arbitrary_self_types_enabled {
@ -1634,7 +1634,7 @@ fn receiver_is_valid<'fcx, 'tcx>(
// The first type is `receiver_ty`, which we know its not equal to `self_ty`; skip it.
autoderef.next();
let receiver_trait_def_id = fcx.tcx.require_lang_item(LangItem::Receiver, None);
let receiver_trait_def_id = tcx.require_lang_item(LangItem::Receiver, None);
// Keep dereferencing `receiver_ty` until we get to `self_ty`.
loop {
@ -1645,12 +1645,12 @@ fn receiver_is_valid<'fcx, 'tcx>(
);
if can_eq_self(potential_self_ty) {
fcx.register_predicates(autoderef.into_obligations());
wfcx.register_obligations(autoderef.into_obligations());
if let Some(mut err) =
fcx.demand_eqtype_with_origin(&cause, self_ty, potential_self_ty)
if let Err(err) =
wfcx.equate_types(&cause, wfcx.param_env, self_ty, potential_self_ty)
{
err.emit();
infcx.report_mismatched_types(&cause, self_ty, potential_self_ty, err).emit();
}
break;
@ -1659,7 +1659,7 @@ fn receiver_is_valid<'fcx, 'tcx>(
// deref chain implement `receiver`
if !arbitrary_self_types_enabled
&& !receiver_is_implemented(
fcx,
wfcx,
receiver_trait_def_id,
cause.clone(),
potential_self_ty,
@ -1678,7 +1678,7 @@ fn receiver_is_valid<'fcx, 'tcx>(
// Without `feature(arbitrary_self_types)`, we require that `receiver_ty` implements `Receiver`.
if !arbitrary_self_types_enabled
&& !receiver_is_implemented(fcx, receiver_trait_def_id, cause.clone(), receiver_ty)
&& !receiver_is_implemented(wfcx, receiver_trait_def_id, cause.clone(), receiver_ty)
{
return false;
}
@ -1687,23 +1687,21 @@ fn receiver_is_valid<'fcx, 'tcx>(
}
fn receiver_is_implemented<'tcx>(
fcx: &FnCtxt<'_, 'tcx>,
wfcx: &WfCheckingCtxt<'_, 'tcx>,
receiver_trait_def_id: DefId,
cause: ObligationCause<'tcx>,
receiver_ty: Ty<'tcx>,
) -> bool {
let tcx = wfcx.tcx();
let trait_ref = ty::Binder::dummy(ty::TraitRef {
def_id: receiver_trait_def_id,
substs: fcx.tcx.mk_substs_trait(receiver_ty, &[]),
substs: tcx.mk_substs_trait(receiver_ty, &[]),
});
let obligation = traits::Obligation::new(
cause,
fcx.param_env,
trait_ref.without_const().to_predicate(fcx.tcx),
);
let obligation =
traits::Obligation::new(cause, wfcx.param_env, trait_ref.without_const().to_predicate(tcx));
if fcx.predicate_must_hold_modulo_regions(&obligation) {
if wfcx.infcx.predicate_must_hold_modulo_regions(&obligation) {
true
} else {
debug!(
@ -1805,55 +1803,56 @@ fn report_bivariance(
err.emit()
}
/// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that
/// aren't true.
fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirId) {
let empty_env = ty::ParamEnv::empty();
impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
/// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that
/// aren't true.
fn check_false_global_bounds(&mut self) {
let tcx = self.ocx.infcx.tcx;
let mut span = self.span;
let empty_env = ty::ParamEnv::empty();
let def_id = fcx.tcx.hir().local_def_id(id);
let predicates_with_span =
fcx.tcx.predicates_of(def_id).predicates.iter().map(|(p, span)| (*p, *span));
// Check elaborated bounds.
let implied_obligations = traits::elaborate_predicates_with_span(fcx.tcx, predicates_with_span);
let def_id = tcx.hir().local_def_id(self.body_id);
let predicates_with_span = tcx.predicates_of(def_id).predicates.iter().copied();
// Check elaborated bounds.
let implied_obligations = traits::elaborate_predicates_with_span(tcx, predicates_with_span);
for obligation in implied_obligations {
// We lower empty bounds like `Vec<dyn Copy>:` as
// `WellFormed(Vec<dyn Copy>)`, which will later get checked by
// regular WF checking
if let ty::PredicateKind::WellFormed(..) = obligation.predicate.kind().skip_binder() {
continue;
}
let pred = obligation.predicate;
// Match the existing behavior.
if pred.is_global() && !pred.has_late_bound_regions() {
let pred = fcx.normalize_associated_types_in(span, pred);
let hir_node = fcx.tcx.hir().find(id);
// only use the span of the predicate clause (#90869)
if let Some(hir::Generics { predicates, .. }) =
hir_node.and_then(|node| node.generics())
{
let obligation_span = obligation.cause.span();
span = predicates
.iter()
// There seems to be no better way to find out which predicate we are in
.find(|pred| pred.span().contains(obligation_span))
.map(|pred| pred.span())
.unwrap_or(obligation_span);
for obligation in implied_obligations {
// We lower empty bounds like `Vec<dyn Copy>:` as
// `WellFormed(Vec<dyn Copy>)`, which will later get checked by
// regular WF checking
if let ty::PredicateKind::WellFormed(..) = obligation.predicate.kind().skip_binder() {
continue;
}
let pred = obligation.predicate;
// Match the existing behavior.
if pred.is_global() && !pred.has_late_bound_regions() {
let pred = self.normalize(span, None, pred);
let hir_node = tcx.hir().find(self.body_id);
let obligation = traits::Obligation::new(
traits::ObligationCause::new(span, id, traits::TrivialBound),
empty_env,
pred,
);
fcx.register_predicate(obligation);
// only use the span of the predicate clause (#90869)
if let Some(hir::Generics { predicates, .. }) =
hir_node.and_then(|node| node.generics())
{
let obligation_span = obligation.cause.span();
span = predicates
.iter()
// There seems to be no better way to find out which predicate we are in
.find(|pred| pred.span().contains(obligation_span))
.map(|pred| pred.span())
.unwrap_or(obligation_span);
}
let obligation = traits::Obligation::new(
traits::ObligationCause::new(span, self.body_id, traits::TrivialBound),
empty_env,
pred,
);
self.ocx.register_obligation(obligation);
}
}
}
fcx.select_all_obligations_or_error();
}
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalDefId) {
@ -1883,17 +1882,16 @@ struct AdtField<'tcx> {
span: Span,
}
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
impl<'a, 'tcx> WfCheckingCtxt<'a, 'tcx> {
// FIXME(eddyb) replace this with getting fields through `ty::AdtDef`.
fn non_enum_variant(&self, struct_def: &hir::VariantData<'_>) -> AdtVariant<'tcx> {
let fields = struct_def
.fields()
.iter()
.map(|field| {
let def_id = self.tcx.hir().local_def_id(field.hir_id);
let field_ty = self.tcx.type_of(def_id);
let field_ty = self.normalize_associated_types_in(field.ty.span, field_ty);
let field_ty = self.resolve_vars_if_possible(field_ty);
let def_id = self.tcx().hir().local_def_id(field.hir_id);
let field_ty = self.tcx().type_of(def_id);
let field_ty = self.normalize(field.ty.span, None, field_ty);
debug!("non_enum_variant: type of field {:?} is {:?}", field, field_ty);
AdtField { ty: field_ty, span: field.ty.span, def_id }
})
@ -1909,7 +1907,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fields: self.non_enum_variant(&variant.data).fields,
explicit_discr: variant
.disr_expr
.map(|explicit_discr| self.tcx.hir().local_def_id(explicit_discr.hir_id)),
.map(|explicit_discr| self.tcx().hir().local_def_id(explicit_discr.hir_id)),
})
.collect()
}

View File

@ -2,17 +2,15 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/builtin-superkinds-self-type.rs:10:16
|
LL | impl <T: Sync> Foo for T { }
| ^^^ ...so that the type `T` will meet its required lifetime bounds...
| ^^^
|
= help: consider adding an explicit lifetime bound `T: 'static`...
= note: ...so that the type `T` will meet its required lifetime bounds...
note: ...that is required by this bound
--> $DIR/builtin-superkinds-self-type.rs:6:24
|
LL | trait Foo : Sized+Sync+'static {
| ^^^^^^^
help: consider adding an explicit lifetime bound...
|
LL | impl <T: Sync + 'static> Foo for T { }
| +++++++++
error: aborting due to previous error

View File

@ -2,45 +2,37 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:10
|
LL | foo: &'static T
| ^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
| ^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | struct Foo<T: 'static> {
| +++++++++
= help: consider adding an explicit lifetime bound `T: 'static`...
= note: ...so that the reference type `&'static T` does not outlive the data it points at
error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
|
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
| ^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | impl<K: 'a> Nested<K> {
| ++++
= help: consider adding an explicit lifetime bound `K: 'a`...
= note: ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
error[E0309]: the parameter type `M` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
|
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
| ^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b + 'a>() {
| ++++
= help: consider adding an explicit lifetime bound `M: 'a`...
= note: ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
|
LL | fn foo<'a, L: X<&'a Nested<K>>>();
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
| ^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | trait X<K: 'a>: Sized {
| ++++
= help: consider adding an explicit lifetime bound `K: 'a`...
= note: ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
error[E0309]: the parameter type `Self` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:28:19
@ -55,12 +47,10 @@ error[E0309]: the parameter type `L` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
|
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
| ^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | fn baz<'a, L: 'a, M: X<&'a Nested<L>>>() {
| ++++
= help: consider adding an explicit lifetime bound `L: 'a`...
= note: ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
error: aborting due to 6 previous errors

View File

@ -2,17 +2,15 @@ error[E0310]: the parameter type `U` may not live long enough
--> $DIR/dont-infer-static.rs:6:10
|
LL | bar: Bar<U>
| ^^^^^^ ...so that the type `U` will meet its required lifetime bounds...
| ^^^^^^
|
= help: consider adding an explicit lifetime bound `U: 'static`...
= note: ...so that the type `U` will meet its required lifetime bounds...
note: ...that is required by this bound
--> $DIR/dont-infer-static.rs:8:15
|
LL | struct Bar<T: 'static> {
| ^^^^^^^
help: consider adding an explicit lifetime bound...
|
LL | struct Foo<U: 'static> {
| +++++++++
error: aborting due to previous error

View File

@ -2,34 +2,28 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:17:18
|
LL | Ref1Variant1(RequireOutlives<'a, T>),
| ^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | enum Ref1<'a, T: 'a> {
| ++++
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the type `T` will meet its required lifetime bounds
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:22:25
|
LL | Ref2Variant2(isize, RequireOutlives<'a, T>),
| ^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | enum Ref2<'a, T: 'a> {
| ++++
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the type `T` will meet its required lifetime bounds
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:35:23
|
LL | RefDoubleVariant1(&'a RequireOutlives<'b, T>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | enum RefDouble<'a, 'b, T: 'b> {
| ++++
= help: consider adding an explicit lifetime bound `T: 'b`...
= note: ...so that the type `T` will meet its required lifetime bounds
error: aborting due to 3 previous errors

View File

@ -2,28 +2,24 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-struct-not-wf.rs:13:16
|
LL | type Out = &'a T;
| ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
| ^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | impl<'a, T: 'a> Trait<'a, T> for usize {
| ++++
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the reference type `&'a T` does not outlive the data it points at
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-struct-not-wf.rs:21:16
|
LL | type Out = RefOk<'a, T>;
| ^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
| ^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the type `T` will meet its required lifetime bounds...
note: ...that is required by this bound
--> $DIR/regions-struct-not-wf.rs:16:20
|
LL | struct RefOk<'a, T:'a> {
| ^^
help: consider adding an explicit lifetime bound...
|
LL | impl<'a, T: 'a> Trait<'a, T> for u32 {
| ++++
error[E0491]: in type `&'a &'b T`, reference has a longer lifetime than the data it references
--> $DIR/regions-struct-not-wf.rs:25:16

View File

@ -2,12 +2,10 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/wf-impl-associated-type-region.rs:10:16
|
LL | type Bar = &'a T;
| ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
| ^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | impl<'a, T: 'a> Foo<'a> for T {
| ++++
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the reference type `&'a T` does not outlive the data it points at
error: aborting due to previous error

View File

@ -2,23 +2,19 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/wf-in-fn-type-static.rs:13:8
|
LL | x: fn() -> &'static T
| ^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
| ^^^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | struct Foo<T: 'static> {
| +++++++++
= help: consider adding an explicit lifetime bound `T: 'static`...
= note: ...so that the reference type `&'static T` does not outlive the data it points at
error[E0310]: the parameter type `T` may not live long enough
--> $DIR/wf-in-fn-type-static.rs:18:8
|
LL | x: fn(&'static T)
| ^^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
| ^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | struct Bar<T: 'static> {
| +++++++++
= help: consider adding an explicit lifetime bound `T: 'static`...
= note: ...so that the reference type `&'static T` does not outlive the data it points at
error: aborting due to 2 previous errors

View File

@ -2,12 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/wf-in-obj-type-static.rs:14:8
|
LL | x: dyn Object<&'static T>
| ^^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | struct Foo<T: 'static> {
| +++++++++
= help: consider adding an explicit lifetime bound `T: 'static`...
= note: ...so that the reference type `&'static T` does not outlive the data it points at
error: aborting due to previous error

View File

@ -2,23 +2,19 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/wf-outlives-ty-in-fn-or-trait.rs:9:16
|
LL | type Out = &'a fn(T);
| ^^^^^^^^^ ...so that the reference type `&'a fn(T)` does not outlive the data it points at
| ^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | impl<'a, T: 'a> Trait<'a, T> for usize {
| ++++
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the reference type `&'a fn(T)` does not outlive the data it points at
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/wf-outlives-ty-in-fn-or-trait.rs:19:16
|
LL | type Out = &'a dyn Baz<T>;
| ^^^^^^^^^^^^^^ ...so that the reference type `&'a (dyn Baz<T> + 'a)` does not outlive the data it points at
| ^^^^^^^^^^^^^^
|
help: consider adding an explicit lifetime bound...
|
LL | impl<'a, T: 'a> Trait<'a, T> for u32 {
| ++++
= help: consider adding an explicit lifetime bound `T: 'a`...
= note: ...so that the reference type `&'a (dyn Baz<T> + 'a)` does not outlive the data it points at
error: aborting due to 2 previous errors