2020-09-19 05:05:42 -07:00
|
|
|
use super::callee::DeferredCallResolution;
|
|
|
|
|
2022-01-27 15:46:59 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2022-08-13 02:15:46 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2020-09-19 05:05:42 -07:00
|
|
|
use rustc_hir as hir;
|
2022-07-12 09:10:22 -05:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-09-19 05:05:42 -07:00
|
|
|
use rustc_hir::HirIdMap;
|
|
|
|
use rustc_infer::infer;
|
2022-09-09 13:01:06 -05:00
|
|
|
use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt};
|
2020-09-19 05:05:42 -07:00
|
|
|
use rustc_middle::ty::fold::TypeFoldable;
|
2022-06-17 13:15:00 +01:00
|
|
|
use rustc_middle::ty::visit::TypeVisitable;
|
2021-07-26 16:57:18 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-07-12 09:10:22 -05:00
|
|
|
use rustc_span::def_id::LocalDefIdMap;
|
2020-09-19 05:05:42 -07:00
|
|
|
use rustc_span::{self, Span};
|
|
|
|
use rustc_trait_selection::infer::InferCtxtExt as _;
|
2022-08-13 02:15:46 +00:00
|
|
|
use rustc_trait_selection::traits::{
|
2022-08-25 23:35:09 +00:00
|
|
|
self, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _,
|
2022-08-13 02:15:46 +00:00
|
|
|
};
|
2020-09-19 05:05:42 -07:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
/// Closures defined within the function. For example:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
|
|
|
/// fn foo() {
|
|
|
|
/// bar(move|| { ... })
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-09-19 05:05:42 -07:00
|
|
|
/// Here, the function `foo()` and the closure passed to
|
|
|
|
/// `bar()` will each have their own `FnCtxt`, but they will
|
|
|
|
/// share the inherited fields.
|
2022-09-09 15:25:29 -05:00
|
|
|
pub struct Inherited<'tcx> {
|
2022-09-09 13:01:06 -05:00
|
|
|
pub(super) infcx: InferCtxt<'tcx>,
|
2020-09-19 05:05:42 -07:00
|
|
|
|
2022-09-09 15:25:29 -05:00
|
|
|
pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
|
2020-09-19 05:05:42 -07:00
|
|
|
|
|
|
|
pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
|
|
|
|
|
|
|
|
pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
|
|
|
|
|
2022-08-24 18:36:44 +00:00
|
|
|
// Some additional `Sized` obligations badly affect type inference.
|
|
|
|
// These obligations are added in a later stage of typeck.
|
2022-08-27 03:42:15 +00:00
|
|
|
// Removing these may also cause additional complications, see #101066.
|
2022-08-24 18:36:44 +00:00
|
|
|
pub(super) deferred_sized_obligations:
|
|
|
|
RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
|
|
|
|
|
2020-09-19 05:05:42 -07:00
|
|
|
// When we process a call like `c()` where `c` is a closure type,
|
|
|
|
// we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
|
|
|
|
// `FnOnce` closure. In that case, we defer full resolution of the
|
|
|
|
// call until upvar inference can kick in and make the
|
|
|
|
// decision. We keep these deferred resolutions grouped by the
|
|
|
|
// def-id of the closure, so that once we decide, we can easily go
|
|
|
|
// back and process them.
|
2022-07-12 09:10:22 -05:00
|
|
|
pub(super) deferred_call_resolutions: RefCell<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
|
2020-09-19 05:05:42 -07:00
|
|
|
|
|
|
|
pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
|
|
|
|
|
2022-10-03 13:49:31 +02:00
|
|
|
pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, hir::HirId)>>,
|
2022-04-14 12:07:36 +00:00
|
|
|
|
|
|
|
pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
|
|
|
|
|
2020-09-19 05:05:42 -07:00
|
|
|
pub(super) deferred_generator_interiors:
|
|
|
|
RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
|
|
|
|
|
|
|
|
pub(super) body_id: Option<hir::BodyId>,
|
2020-11-23 07:43:33 -05:00
|
|
|
|
|
|
|
/// Whenever we introduce an adjustment from `!` into a type variable,
|
|
|
|
/// we record that type variable here. This is later used to inform
|
|
|
|
/// fallback. See the `fallback` module for details.
|
2022-01-27 15:46:59 +00:00
|
|
|
pub(super) diverging_type_vars: RefCell<FxHashSet<Ty<'tcx>>>,
|
2020-09-19 05:05:42 -07:00
|
|
|
}
|
|
|
|
|
2022-09-09 15:25:29 -05:00
|
|
|
impl<'tcx> Deref for Inherited<'tcx> {
|
2022-09-09 13:01:06 -05:00
|
|
|
type Target = InferCtxt<'tcx>;
|
2020-09-19 05:05:42 -07:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.infcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:08:57 +02:00
|
|
|
/// A temporary returned by `Inherited::build(...)`. This is necessary
|
2022-09-09 15:08:06 -05:00
|
|
|
/// for multiple `InferCtxt` to share the same `typeck_results`
|
2022-04-01 12:08:57 +02:00
|
|
|
/// without using `Rc` or something similar.
|
2020-09-19 05:05:42 -07:00
|
|
|
pub struct InheritedBuilder<'tcx> {
|
|
|
|
infcx: infer::InferCtxtBuilder<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
2022-09-09 13:01:06 -05:00
|
|
|
typeck_results: RefCell<ty::TypeckResults<'tcx>>,
|
2020-09-19 05:05:42 -07:00
|
|
|
}
|
|
|
|
|
2022-09-09 15:25:29 -05:00
|
|
|
impl<'tcx> Inherited<'tcx> {
|
2020-09-19 05:05:42 -07:00
|
|
|
pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
|
|
|
|
let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
|
|
|
|
|
|
|
|
InheritedBuilder {
|
2022-07-20 11:40:15 +02:00
|
|
|
infcx: tcx
|
|
|
|
.infer_ctxt()
|
|
|
|
.ignoring_regions()
|
2022-09-09 13:01:06 -05:00
|
|
|
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id))
|
2022-08-13 02:15:46 +00:00
|
|
|
.with_normalize_fn_sig_for_diagnostic(Lrc::new(move |infcx, fn_sig| {
|
|
|
|
if fn_sig.has_escaping_bound_vars() {
|
|
|
|
return fn_sig;
|
|
|
|
}
|
|
|
|
infcx.probe(|_| {
|
2022-08-25 23:35:09 +00:00
|
|
|
let ocx = ObligationCtxt::new_in_snapshot(infcx);
|
|
|
|
let normalized_fn_sig = ocx.normalize(
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
// FIXME(compiler-errors): This is probably not the right param-env...
|
|
|
|
infcx.tcx.param_env(def_id),
|
|
|
|
fn_sig,
|
|
|
|
);
|
|
|
|
if ocx.select_all_or_error().is_empty() {
|
2022-08-17 22:00:53 +00:00
|
|
|
let normalized_fn_sig =
|
|
|
|
infcx.resolve_vars_if_possible(normalized_fn_sig);
|
|
|
|
if !normalized_fn_sig.needs_infer() {
|
|
|
|
return normalized_fn_sig;
|
|
|
|
}
|
2022-08-13 02:15:46 +00:00
|
|
|
}
|
2022-08-17 22:00:53 +00:00
|
|
|
fn_sig
|
2022-08-13 02:15:46 +00:00
|
|
|
})
|
|
|
|
})),
|
2020-09-19 05:05:42 -07:00
|
|
|
def_id,
|
2022-09-09 13:01:06 -05:00
|
|
|
typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
|
2020-09-19 05:05:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> InheritedBuilder<'tcx> {
|
2022-09-09 15:25:29 -05:00
|
|
|
pub fn enter<F, R>(mut self, f: F) -> R
|
2020-09-19 05:05:42 -07:00
|
|
|
where
|
2022-09-09 15:25:29 -05:00
|
|
|
F: FnOnce(&Inherited<'tcx>) -> R,
|
2020-09-19 05:05:42 -07:00
|
|
|
{
|
|
|
|
let def_id = self.def_id;
|
2022-09-19 22:03:59 -05:00
|
|
|
f(&Inherited::new(self.infcx.build(), def_id, self.typeck_results))
|
2020-09-19 05:05:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:25:29 -05:00
|
|
|
impl<'tcx> Inherited<'tcx> {
|
2022-09-09 13:01:06 -05:00
|
|
|
fn new(
|
|
|
|
infcx: InferCtxt<'tcx>,
|
|
|
|
def_id: LocalDefId,
|
2022-09-09 15:25:29 -05:00
|
|
|
typeck_results: RefCell<ty::TypeckResults<'tcx>>,
|
2022-09-09 13:01:06 -05:00
|
|
|
) -> Self {
|
2020-09-19 05:05:42 -07:00
|
|
|
let tcx = infcx.tcx;
|
2022-07-15 23:13:04 -04:00
|
|
|
let body_id = tcx.hir().maybe_body_owned_by(def_id);
|
2020-09-19 05:05:42 -07:00
|
|
|
|
|
|
|
Inherited {
|
2022-07-25 13:11:07 +02:00
|
|
|
typeck_results,
|
2020-09-19 05:05:42 -07:00
|
|
|
infcx,
|
2022-07-20 11:40:15 +02:00
|
|
|
fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
|
2020-09-19 05:05:42 -07:00
|
|
|
locals: RefCell::new(Default::default()),
|
2022-08-24 18:36:44 +00:00
|
|
|
deferred_sized_obligations: RefCell::new(Vec::new()),
|
2020-09-19 05:05:42 -07:00
|
|
|
deferred_call_resolutions: RefCell::new(Default::default()),
|
|
|
|
deferred_cast_checks: RefCell::new(Vec::new()),
|
2022-04-14 12:07:36 +00:00
|
|
|
deferred_transmute_checks: RefCell::new(Vec::new()),
|
|
|
|
deferred_asm_checks: RefCell::new(Vec::new()),
|
2020-09-19 05:05:42 -07:00
|
|
|
deferred_generator_interiors: RefCell::new(Vec::new()),
|
2020-11-23 07:43:33 -05:00
|
|
|
diverging_type_vars: RefCell::new(Default::default()),
|
2020-09-19 05:05:42 -07:00
|
|
|
body_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 16:10:22 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2021-07-26 12:06:32 +08:00
|
|
|
pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
|
2020-09-19 05:05:42 -07:00
|
|
|
if obligation.has_escaping_bound_vars() {
|
|
|
|
span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
|
|
|
|
}
|
|
|
|
self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn register_predicates<I>(&self, obligations: I)
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
|
|
|
|
{
|
|
|
|
for obligation in obligations {
|
|
|
|
self.register_predicate(obligation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
|
|
|
|
self.register_predicates(infer_ok.obligations);
|
|
|
|
infer_ok.value
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn normalize_associated_types_in<T>(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
body_id: hir::HirId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-10-24 02:21:18 +02:00
|
|
|
value: T,
|
2020-09-19 05:05:42 -07:00
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
Add initial implementation of HIR-based WF checking for diagnostics
During well-formed checking, we walk through all types 'nested' in
generic arguments. For example, WF-checking `Option<MyStruct<u8>>`
will cause us to check `MyStruct<u8>` and `u8`. However, this is done
on a `rustc_middle::ty::Ty`, which has no span information. As a result,
any errors that occur will have a very general span (e.g. the
definintion of an associated item).
This becomes a problem when macros are involved. In general, an
associated type like `type MyType = Option<MyStruct<u8>>;` may
have completely different spans for each nested type in the HIR. Using
the span of the entire associated item might end up pointing to a macro
invocation, even though a user-provided span is available in one of the
nested types.
This PR adds a framework for HIR-based well formed checking. This check
is only run during error reporting, and is used to obtain a more precise
span for an existing error. This is accomplished by individually
checking each 'nested' type in the HIR for the type, allowing us to
find the most-specific type (and span) that produces a given error.
The majority of the changes are to the error-reporting code. However,
some of the general trait code is modified to pass through more
information.
Since this has no soundness implications, I've implemented a minimal
version to begin with, which can be extended over time. In particular,
this only works for HIR items with a corresponding `DefId` (e.g. it will
not work for WF-checking performed within function bodies).
2021-04-04 16:55:39 -04:00
|
|
|
self.normalize_associated_types_in_with_cause(
|
|
|
|
ObligationCause::misc(span, body_id),
|
|
|
|
param_env,
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn normalize_associated_types_in_with_cause<T>(
|
|
|
|
&self,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
value: T,
|
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
let ok = self.partially_normalize_associated_types_in(cause, param_env, value);
|
2021-07-16 16:23:42 -04:00
|
|
|
debug!(?ok);
|
2020-09-19 05:05:42 -07:00
|
|
|
self.register_infer_ok_obligations(ok)
|
|
|
|
}
|
|
|
|
}
|