2020-03-05 21:07:42 +00:00
|
|
|
//! Trait Resolution. See the [rustc dev guide] for more information on how this works.
|
2020-01-22 07:51:01 +00:00
|
|
|
//!
|
2020-03-09 21:33:04 +00:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-03-03 16:25:03 +00:00
|
|
|
mod chalk;
|
2020-01-22 08:04:50 +00:00
|
|
|
pub mod query;
|
2020-01-22 08:01:22 +00:00
|
|
|
pub mod select;
|
2020-01-22 12:44:59 +00:00
|
|
|
pub mod specialization_graph;
|
2020-01-22 12:42:04 +00:00
|
|
|
mod structural_impls;
|
2021-11-22 04:43:48 +00:00
|
|
|
pub mod util;
|
2020-01-22 08:01:22 +00:00
|
|
|
|
2020-03-03 16:25:03 +00:00
|
|
|
use crate::infer::canonical::Canonical;
|
2022-07-09 09:35:06 +00:00
|
|
|
use crate::ty::abstract_const::NotConstEvaluatable;
|
2020-01-22 07:51:01 +00:00
|
|
|
use crate::ty::subst::SubstsRef;
|
2022-09-04 22:21:15 +00:00
|
|
|
use crate::ty::{self, AdtKind, Ty, TyCtxt};
|
2020-01-22 07:51:01 +00:00
|
|
|
|
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 20:55:39 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2022-01-23 20:41:46 +00:00
|
|
|
use rustc_errors::{Applicability, Diagnostic};
|
2020-01-22 07:51:01 +00:00
|
|
|
use rustc_hir as hir;
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 16:33:49 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2020-01-22 07:51:01 +00:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2020-02-10 18:55:49 +00:00
|
|
|
use smallvec::SmallVec;
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-02-10 18:55:49 +00:00
|
|
|
use std::borrow::Cow;
|
2021-11-14 22:49:57 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-01-22 08:01:22 +00:00
|
|
|
pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache};
|
|
|
|
|
2020-09-01 15:58:34 +00:00
|
|
|
pub type CanonicalChalkEnvironmentAndGoal<'tcx> = Canonical<'tcx, ChalkEnvironmentAndGoal<'tcx>>;
|
2020-03-03 16:25:03 +00:00
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
pub use self::ObligationCauseCode::*;
|
|
|
|
|
2020-09-01 15:58:34 +00:00
|
|
|
pub use self::chalk::{ChalkEnvironmentAndGoal, RustInterner as ChalkRustInterner};
|
2020-03-03 16:25:03 +00:00
|
|
|
|
2020-01-22 07:54:46 +00:00
|
|
|
/// Depending on the stage of compilation, we want projection to be
|
|
|
|
/// more or less conservative.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, HashStable)]
|
|
|
|
pub enum Reveal {
|
|
|
|
/// At type-checking time, we refuse to project any associated
|
|
|
|
/// type that is marked `default`. Non-`default` ("final") types
|
|
|
|
/// are always projected. This is necessary in general for
|
|
|
|
/// soundness of specialization. However, we *could* allow
|
|
|
|
/// projections in fully-monomorphic cases. We choose not to,
|
|
|
|
/// because we prefer for `default type` to force the type
|
|
|
|
/// definition to be treated abstractly by any consumers of the
|
|
|
|
/// impl. Concretely, that means that the following example will
|
|
|
|
/// fail to compile:
|
|
|
|
///
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```compile_fail,E0308
|
|
|
|
/// #![feature(specialization)]
|
2020-01-22 07:54:46 +00:00
|
|
|
/// trait Assoc {
|
|
|
|
/// type Output;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl<T> Assoc for T {
|
|
|
|
/// default type Output = bool;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2022-04-15 22:04:34 +00:00
|
|
|
/// let x: <() as Assoc>::Output = true;
|
2020-01-22 07:54:46 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2022-04-12 14:02:57 +00:00
|
|
|
///
|
|
|
|
/// We also do not reveal the hidden type of opaque types during
|
|
|
|
/// type-checking.
|
2020-01-22 07:54:46 +00:00
|
|
|
UserFacing,
|
|
|
|
|
|
|
|
/// At codegen time, all monomorphic projections will succeed.
|
|
|
|
/// Also, `impl Trait` is normalized to the concrete type,
|
|
|
|
/// which has to be already collected by type-checking.
|
|
|
|
///
|
|
|
|
/// NOTE: as `impl Trait`'s concrete type should *never*
|
|
|
|
/// be observable directly by the user, `Reveal::All`
|
|
|
|
/// should not be used by checks which may expose
|
|
|
|
/// type equality or type contents to the user.
|
2020-11-24 23:44:04 +00:00
|
|
|
/// There are some exceptions, e.g., around auto traits and
|
2020-01-22 07:54:46 +00:00
|
|
|
/// transmute-checking, which expose some details, but
|
|
|
|
/// not the whole concrete type of the `impl Trait`.
|
|
|
|
All,
|
|
|
|
}
|
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
/// The reason why we incurred this obligation; used for error reporting.
|
2020-06-03 21:59:30 +00:00
|
|
|
///
|
2021-11-11 01:01:12 +00:00
|
|
|
/// Non-misc `ObligationCauseCode`s are stored on the heap. This gives the
|
|
|
|
/// best trade-off between keeping the type small (which makes copies cheaper)
|
|
|
|
/// while not doing too many heap allocations.
|
2020-06-03 21:59:30 +00:00
|
|
|
///
|
|
|
|
/// We do not want to intern this as there are a lot of obligation causes which
|
|
|
|
/// only live for a short period of time.
|
2021-11-14 22:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Lift)]
|
2021-11-11 01:01:12 +00:00
|
|
|
pub struct ObligationCause<'tcx> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub span: Span,
|
|
|
|
|
|
|
|
/// The ID of the fn body that triggered this obligation. This is
|
|
|
|
/// used for region obligations to determine the precise
|
|
|
|
/// environment in which the region obligation should be evaluated
|
|
|
|
/// (in particular, closures can add new assumptions). See the
|
|
|
|
/// field `region_obligations` of the `FulfillmentContext` for more
|
|
|
|
/// information.
|
|
|
|
pub body_id: hir::HirId,
|
|
|
|
|
2022-05-10 12:01:56 +00:00
|
|
|
code: InternedObligationCauseCode<'tcx>,
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 01:01:12 +00:00
|
|
|
// This custom hash function speeds up hashing for `Obligation` deduplication
|
|
|
|
// greatly by skipping the `code` field, which can be large and complex. That
|
|
|
|
// shouldn't affect hash quality much since there are several other fields in
|
|
|
|
// `Obligation` which should be unique enough, especially the predicate itself
|
|
|
|
// which is hashed as an interned pointer. See #90996.
|
|
|
|
impl Hash for ObligationCause<'_> {
|
2021-11-14 22:49:57 +00:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.body_id.hash(state);
|
|
|
|
self.span.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
impl<'tcx> ObligationCause<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(
|
|
|
|
span: Span,
|
|
|
|
body_id: hir::HirId,
|
|
|
|
code: ObligationCauseCode<'tcx>,
|
|
|
|
) -> ObligationCause<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
ObligationCause { span, body_id, code: code.into() }
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn misc(span: Span, body_id: hir::HirId) -> ObligationCause<'tcx> {
|
2020-06-03 21:59:30 +00:00
|
|
|
ObligationCause::new(span, body_id, MiscObligation)
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 21:59:30 +00:00
|
|
|
#[inline(always)]
|
2020-01-22 07:51:01 +00:00
|
|
|
pub fn dummy() -> ObligationCause<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
ObligationCause::dummy_with_span(DUMMY_SP)
|
2021-11-11 01:01:12 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 11:29:01 +00:00
|
|
|
#[inline(always)]
|
2021-11-11 01:01:12 +00:00
|
|
|
pub fn dummy_with_span(span: Span) -> ObligationCause<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
ObligationCause { span, body_id: hir::CRATE_HIR_ID, code: Default::default() }
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 02:02:08 +00:00
|
|
|
pub fn span(&self) -> Span {
|
2021-11-11 01:01:12 +00:00
|
|
|
match *self.code() {
|
2020-01-22 07:51:01 +00:00
|
|
|
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
|
|
|
|
arm_span,
|
|
|
|
..
|
|
|
|
}) => arm_span,
|
|
|
|
_ => self.span,
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 01:01:12 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn code(&self) -> &ObligationCauseCode<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
&self.code
|
2021-11-11 01:01:12 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 08:43:39 +00:00
|
|
|
pub fn map_code(
|
|
|
|
&mut self,
|
2022-05-10 12:01:56 +00:00
|
|
|
f: impl FnOnce(InternedObligationCauseCode<'tcx>) -> ObligationCauseCode<'tcx>,
|
2022-05-10 08:43:39 +00:00
|
|
|
) {
|
2022-05-10 12:01:56 +00:00
|
|
|
self.code = f(std::mem::take(&mut self.code)).into();
|
2022-05-10 08:43:39 +00:00
|
|
|
}
|
2022-05-10 09:26:09 +00:00
|
|
|
|
|
|
|
pub fn derived_cause(
|
|
|
|
mut self,
|
|
|
|
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
2022-05-10 10:32:35 +00:00
|
|
|
variant: impl FnOnce(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
|
2022-05-10 09:26:09 +00:00
|
|
|
) -> ObligationCause<'tcx> {
|
|
|
|
/*!
|
|
|
|
* Creates a cause for obligations that are derived from
|
|
|
|
* `obligation` by a recursive search (e.g., for a builtin
|
|
|
|
* bound, or eventually a `auto trait Foo`). If `obligation`
|
|
|
|
* is itself a derived obligation, this is just a clone, but
|
|
|
|
* otherwise we create a "derived obligation" cause so as to
|
|
|
|
* keep track of the original root obligation for error
|
|
|
|
* reporting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NOTE(flaper87): As of now, it keeps track of the whole error
|
|
|
|
// chain. Ideally, we should have a way to configure this either
|
|
|
|
// by using -Z verbose or just a CLI argument.
|
2022-05-10 12:01:56 +00:00
|
|
|
self.code =
|
|
|
|
variant(DerivedObligationCause { parent_trait_pred, parent_code: self.code }).into();
|
2022-05-10 09:26:09 +00:00
|
|
|
self
|
2021-11-11 01:01:12 +00:00
|
|
|
}
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 13:42:08 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
|
2020-07-01 00:41:15 +00:00
|
|
|
pub struct UnifyReceiverContext<'tcx> {
|
|
|
|
pub assoc_item: ty::AssocItem,
|
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
|
|
|
pub substs: SubstsRef<'tcx>,
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:01:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, Default)]
|
2022-05-10 11:26:53 +00:00
|
|
|
pub struct InternedObligationCauseCode<'tcx> {
|
2022-05-22 19:48:19 +00:00
|
|
|
/// `None` for `ObligationCauseCode::MiscObligation` (a common case, occurs ~60% of
|
2022-05-10 12:01:56 +00:00
|
|
|
/// the time). `Some` otherwise.
|
2022-05-10 11:26:53 +00:00
|
|
|
code: Option<Lrc<ObligationCauseCode<'tcx>>>,
|
|
|
|
}
|
|
|
|
|
2022-05-16 13:34:03 +00:00
|
|
|
impl<'tcx> ObligationCauseCode<'tcx> {
|
2022-05-12 11:29:01 +00:00
|
|
|
#[inline(always)]
|
2022-05-16 13:34:03 +00:00
|
|
|
fn into(self) -> InternedObligationCauseCode<'tcx> {
|
|
|
|
InternedObligationCauseCode {
|
2022-05-22 19:48:19 +00:00
|
|
|
code: if let ObligationCauseCode::MiscObligation = self {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Lrc::new(self))
|
|
|
|
},
|
2022-05-13 09:30:09 +00:00
|
|
|
}
|
2022-05-10 12:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:26:53 +00:00
|
|
|
impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> {
|
|
|
|
type Target = ObligationCauseCode<'tcx>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2022-05-22 19:48:19 +00:00
|
|
|
self.code.as_deref().unwrap_or(&ObligationCauseCode::MiscObligation)
|
2022-05-10 11:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:42:08 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
|
2020-01-22 07:51:01 +00:00
|
|
|
pub enum ObligationCauseCode<'tcx> {
|
|
|
|
/// Not well classified or should be obvious from the span.
|
|
|
|
MiscObligation,
|
|
|
|
|
|
|
|
/// A slice or array is WF only if `T: Sized`.
|
|
|
|
SliceOrArrayElem,
|
|
|
|
|
|
|
|
/// A tuple is WF only if its middle elements are `Sized`.
|
|
|
|
TupleElem,
|
|
|
|
|
|
|
|
/// This is the trait reference from the given projection.
|
|
|
|
ProjectionWf(ty::ProjectionTy<'tcx>),
|
|
|
|
|
2022-08-17 16:20:22 +00:00
|
|
|
/// Must satisfy all of the where-clause predicates of the
|
|
|
|
/// given item.
|
2020-01-22 07:51:01 +00:00
|
|
|
ItemObligation(DefId),
|
|
|
|
|
2022-08-17 16:20:22 +00:00
|
|
|
/// Like `ItemObligation`, but carries the span of the
|
|
|
|
/// predicate when it can be identified.
|
2020-01-22 07:51:01 +00:00
|
|
|
BindingObligation(DefId, Span),
|
|
|
|
|
2022-08-17 16:20:22 +00:00
|
|
|
/// Like `ItemObligation`, but carries the `HirId` of the
|
|
|
|
/// expression that caused the obligation, and the `usize`
|
|
|
|
/// indicates exactly which predicate it is in the list of
|
|
|
|
/// instantiated predicates.
|
|
|
|
ExprItemObligation(DefId, rustc_hir::HirId, usize),
|
|
|
|
|
|
|
|
/// Combines `ExprItemObligation` and `BindingObligation`.
|
2022-08-16 06:27:22 +00:00
|
|
|
ExprBindingObligation(DefId, Span, rustc_hir::HirId, usize),
|
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
/// A type like `&'a T` is WF only if `T: 'a`.
|
|
|
|
ReferenceOutlivesReferent(Ty<'tcx>),
|
|
|
|
|
|
|
|
/// A type like `Box<Foo<'a> + 'b>` is WF only if `'b: 'a`.
|
|
|
|
ObjectTypeBound(Ty<'tcx>, ty::Region<'tcx>),
|
|
|
|
|
|
|
|
/// Obligation incurred due to an object cast.
|
2022-06-20 03:49:07 +00:00
|
|
|
ObjectCastObligation(/* Concrete type */ Ty<'tcx>, /* Object type */ Ty<'tcx>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Obligation incurred due to a coercion.
|
|
|
|
Coercion {
|
|
|
|
source: Ty<'tcx>,
|
|
|
|
target: Ty<'tcx>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Various cases where expressions must be `Sized` / `Copy` / etc.
|
|
|
|
/// `L = X` implies that `L` is `Sized`.
|
|
|
|
AssignmentLhsSized,
|
|
|
|
/// `(x1, .., xn)` must be `Sized`.
|
|
|
|
TupleInitializerSized,
|
|
|
|
/// `S { ... }` must be `Sized`.
|
|
|
|
StructInitializerSized,
|
|
|
|
/// Type of each variable must be `Sized`.
|
|
|
|
VariableType(hir::HirId),
|
|
|
|
/// Argument type must be `Sized`.
|
2020-07-10 21:53:48 +00:00
|
|
|
SizedArgumentType(Option<Span>),
|
2020-01-22 07:51:01 +00:00
|
|
|
/// Return type must be `Sized`.
|
|
|
|
SizedReturnType,
|
|
|
|
/// Yield type must be `Sized`.
|
|
|
|
SizedYieldType,
|
2021-08-20 14:25:52 +00:00
|
|
|
/// Box expression result type must be `Sized`.
|
|
|
|
SizedBoxType,
|
2020-02-13 11:00:55 +00:00
|
|
|
/// Inline asm operand type must be `Sized`.
|
|
|
|
InlineAsmSized,
|
2022-03-31 10:50:53 +00:00
|
|
|
/// `[expr; N]` requires `type_of(expr): Copy`.
|
|
|
|
RepeatElementCopy {
|
|
|
|
/// If element is a `const fn` we display a help message suggesting to move the
|
|
|
|
/// function call to a new `const` item while saying that `T` doesn't implement `Copy`.
|
|
|
|
is_const_fn: bool,
|
|
|
|
},
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Types of fields (other than the last, except for packed structs) in a struct must be sized.
|
|
|
|
FieldSized {
|
|
|
|
adt_kind: AdtKind,
|
2020-07-10 22:13:49 +00:00
|
|
|
span: Span,
|
2020-01-22 07:51:01 +00:00
|
|
|
last: bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Constant expressions must be sized.
|
|
|
|
ConstSized,
|
|
|
|
|
|
|
|
/// `static` items must have `Sync` type.
|
|
|
|
SharedStatic,
|
|
|
|
|
|
|
|
BuiltinDerivedObligation(DerivedObligationCause<'tcx>),
|
|
|
|
|
2021-10-13 13:58:41 +00:00
|
|
|
ImplDerivedObligation(Box<ImplDerivedObligationCause<'tcx>>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-04-11 21:14:16 +00:00
|
|
|
DerivedObligation(DerivedObligationCause<'tcx>),
|
|
|
|
|
2021-09-07 11:19:57 +00:00
|
|
|
FunctionArgumentObligation {
|
|
|
|
/// The node of the relevant argument in the function call.
|
|
|
|
arg_hir_id: hir::HirId,
|
|
|
|
/// The node of the function call.
|
|
|
|
call_hir_id: hir::HirId,
|
|
|
|
/// The obligation introduced by this argument.
|
2022-05-10 11:26:53 +00:00
|
|
|
parent_code: InternedObligationCauseCode<'tcx>,
|
2021-09-07 11:19:57 +00:00
|
|
|
},
|
|
|
|
|
2020-04-14 22:12:11 +00:00
|
|
|
/// Error derived when matching traits/impls; see ObligationCause for more details
|
2022-07-24 19:33:26 +00:00
|
|
|
CompareImplItemObligation {
|
2022-04-01 11:38:43 +00:00
|
|
|
impl_item_def_id: LocalDefId,
|
2020-01-22 07:51:01 +00:00
|
|
|
trait_item_def_id: DefId,
|
2022-07-24 19:33:26 +00:00
|
|
|
kind: ty::AssocKind,
|
2020-01-22 07:51:01 +00:00
|
|
|
},
|
|
|
|
|
2022-01-09 04:30:19 +00:00
|
|
|
/// Checking that the bounds of a trait's associated type hold for a given impl
|
|
|
|
CheckAssociatedTypeBounds {
|
2022-04-01 11:38:43 +00:00
|
|
|
impl_item_def_id: LocalDefId,
|
2022-01-09 04:30:19 +00:00
|
|
|
trait_item_def_id: DefId,
|
|
|
|
},
|
|
|
|
|
2022-04-01 11:47:01 +00:00
|
|
|
/// Checking that this expression can be assigned to its target.
|
2020-01-22 07:51:01 +00:00
|
|
|
ExprAssignable,
|
|
|
|
|
|
|
|
/// Computing common supertype in the arms of a match expression
|
|
|
|
MatchExpressionArm(Box<MatchExpressionArmCause<'tcx>>),
|
|
|
|
|
|
|
|
/// Type error arising from type checking a pattern against an expected type.
|
|
|
|
Pattern {
|
|
|
|
/// The span of the scrutinee or type expression which caused the `root_ty` type.
|
|
|
|
span: Option<Span>,
|
|
|
|
/// The root expected type induced by a scrutinee or type expression.
|
|
|
|
root_ty: Ty<'tcx>,
|
|
|
|
/// Whether the `Span` came from an expression or a type expression.
|
|
|
|
origin_expr: bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Constants in patterns must have `Structural` type.
|
|
|
|
ConstPatternStructural,
|
|
|
|
|
|
|
|
/// Computing common supertype in an if expression
|
2022-07-21 00:03:02 +00:00
|
|
|
IfExpression(Box<IfExpressionCause<'tcx>>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Computing common supertype of an if expression with no else counter-part
|
|
|
|
IfExpressionWithNoElse,
|
|
|
|
|
|
|
|
/// `main` has wrong type
|
|
|
|
MainFunctionType,
|
|
|
|
|
|
|
|
/// `start` has wrong type
|
|
|
|
StartFunctionType,
|
|
|
|
|
|
|
|
/// Intrinsic has wrong type
|
|
|
|
IntrinsicType,
|
|
|
|
|
2021-07-30 16:30:12 +00:00
|
|
|
/// A let else block does not diverge
|
|
|
|
LetElse,
|
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
/// Method receiver
|
|
|
|
MethodReceiver,
|
|
|
|
|
2020-07-01 00:41:15 +00:00
|
|
|
UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
|
2020-06-28 22:26:12 +00:00
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
/// `return` with no expression
|
|
|
|
ReturnNoExpression,
|
|
|
|
|
|
|
|
/// `return` with an expression
|
|
|
|
ReturnValue(hir::HirId),
|
|
|
|
|
|
|
|
/// Return type of this function
|
|
|
|
ReturnType,
|
|
|
|
|
2022-06-07 06:20:13 +00:00
|
|
|
/// Opaque return type of this function
|
|
|
|
OpaqueReturnType(Option<(Ty<'tcx>, Span)>),
|
|
|
|
|
2020-01-22 07:51:01 +00:00
|
|
|
/// Block implicit return
|
|
|
|
BlockTailExpression(hir::HirId),
|
|
|
|
|
|
|
|
/// #[feature(trivial_bounds)] is not enabled
|
|
|
|
TrivialBound,
|
2021-01-08 22:07:49 +00:00
|
|
|
|
|
|
|
/// If `X` is the concrete type of an opaque type `impl Y`, then `X` must implement `Y`
|
|
|
|
OpaqueType,
|
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 20:55:39 +00:00
|
|
|
|
2021-11-16 20:07:23 +00:00
|
|
|
AwaitableExpr(Option<hir::HirId>),
|
2021-11-16 00:57:53 +00:00
|
|
|
|
2021-11-16 01:46:28 +00:00
|
|
|
ForLoopIterator,
|
|
|
|
|
2021-11-16 02:17:57 +00:00
|
|
|
QuestionMark,
|
|
|
|
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 16:33:49 +00:00
|
|
|
/// Well-formed checking. If a `WellFormedLoc` is provided,
|
2022-06-06 06:04:37 +00:00
|
|
|
/// then it will be used to perform HIR-based wf checking
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 16:33:49 +00:00
|
|
|
/// after an error occurs, in order to generate a more precise error span.
|
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 20:55:39 +00:00
|
|
|
/// This is purely for diagnostic purposes - it is always
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 16:33:49 +00:00
|
|
|
/// correct to use `MiscObligation` instead, or to specify
|
|
|
|
/// `WellFormed(None)`
|
|
|
|
WellFormed(Option<WellFormedLoc>),
|
2021-07-15 14:03:39 +00:00
|
|
|
|
|
|
|
/// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching against.
|
2021-09-16 02:58:40 +00:00
|
|
|
MatchImpl(ObligationCause<'tcx>, DefId),
|
2022-02-17 10:33:32 +00:00
|
|
|
|
|
|
|
BinOp {
|
|
|
|
rhs_span: Option<Span>,
|
|
|
|
is_lit: bool,
|
2022-09-04 22:21:15 +00:00
|
|
|
output_ty: Option<Ty<'tcx>>,
|
2022-02-17 10:33:32 +00:00
|
|
|
},
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 16:33:49 +00:00
|
|
|
/// The 'location' at which we try to perform HIR-based wf checking.
|
|
|
|
/// This information is used to obtain an `hir::Ty`, which
|
|
|
|
/// we can walk in order to obtain precise spans for any
|
|
|
|
/// 'nested' types (e.g. `Foo` in `Option<Foo>`).
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
|
|
|
|
pub enum WellFormedLoc {
|
|
|
|
/// Use the type of the provided definition.
|
|
|
|
Ty(LocalDefId),
|
|
|
|
/// Use the type of the parameter of the provided function.
|
|
|
|
/// We cannot use `hir::Param`, since the function may
|
|
|
|
/// not have a body (e.g. a trait method definition)
|
|
|
|
Param {
|
|
|
|
/// The function to lookup the parameter in
|
|
|
|
function: LocalDefId,
|
|
|
|
/// The index of the parameter to use.
|
|
|
|
/// Parameters are indexed from 0, with the return type
|
|
|
|
/// being the last 'parameter'
|
|
|
|
param_idx: u16,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-13 13:58:41 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
|
|
|
|
pub struct ImplDerivedObligationCause<'tcx> {
|
|
|
|
pub derived: DerivedObligationCause<'tcx>,
|
|
|
|
pub impl_def_id: DefId,
|
|
|
|
pub span: Span,
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:03:52 +00:00
|
|
|
impl<'tcx> ObligationCauseCode<'tcx> {
|
2020-01-22 07:51:01 +00:00
|
|
|
// Return the base obligation, ignoring derived obligations.
|
|
|
|
pub fn peel_derives(&self) -> &Self {
|
|
|
|
let mut base_cause = self;
|
2022-05-10 11:03:52 +00:00
|
|
|
while let Some((parent_code, _)) = base_cause.parent() {
|
|
|
|
base_cause = parent_code;
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
base_cause
|
|
|
|
}
|
2022-05-10 11:03:52 +00:00
|
|
|
|
|
|
|
pub fn parent(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
|
|
|
|
match self {
|
|
|
|
FunctionArgumentObligation { parent_code, .. } => Some((parent_code, None)),
|
|
|
|
BuiltinDerivedObligation(derived)
|
|
|
|
| DerivedObligation(derived)
|
|
|
|
| ImplDerivedObligation(box ImplDerivedObligationCause { derived, .. }) => {
|
2022-05-10 12:01:56 +00:00
|
|
|
Some((&derived.parent_code, Some(derived.parent_trait_pred)))
|
2022-05-10 11:03:52 +00:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 03:13:45 +00:00
|
|
|
|
|
|
|
pub fn peel_match_impls(&self) -> &Self {
|
|
|
|
match self {
|
|
|
|
MatchImpl(cause, _) => cause.code(),
|
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-12-24 14:50:44 +00:00
|
|
|
static_assert_size!(ObligationCauseCode<'_>, 48);
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-10-22 17:27:40 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum StatementAsExpression {
|
|
|
|
CorrectType,
|
|
|
|
NeedsBoxing,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ty::Lift<'tcx> for StatementAsExpression {
|
|
|
|
type Lifted = StatementAsExpression;
|
|
|
|
fn lift_to_tcx(self, _tcx: TyCtxt<'tcx>) -> Option<StatementAsExpression> {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:42:08 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
|
2020-01-22 07:51:01 +00:00
|
|
|
pub struct MatchExpressionArmCause<'tcx> {
|
2022-07-21 01:26:00 +00:00
|
|
|
pub arm_block_id: Option<hir::HirId>,
|
|
|
|
pub arm_ty: Ty<'tcx>,
|
2020-01-22 07:51:01 +00:00
|
|
|
pub arm_span: Span,
|
2022-07-21 01:26:00 +00:00
|
|
|
pub prior_arm_block_id: Option<hir::HirId>,
|
|
|
|
pub prior_arm_ty: Ty<'tcx>,
|
|
|
|
pub prior_arm_span: Span,
|
2020-10-22 21:20:02 +00:00
|
|
|
pub scrut_span: Span,
|
2020-01-22 07:51:01 +00:00
|
|
|
pub source: hir::MatchSource,
|
|
|
|
pub prior_arms: Vec<Span>,
|
|
|
|
pub scrut_hir_id: hir::HirId,
|
2020-08-23 00:24:26 +00:00
|
|
|
pub opt_suggest_box_span: Option<Span>,
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 00:03:02 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
#[derive(Lift, TypeFoldable, TypeVisitable)]
|
|
|
|
pub struct IfExpressionCause<'tcx> {
|
|
|
|
pub then_id: hir::HirId,
|
|
|
|
pub else_id: hir::HirId,
|
|
|
|
pub then_ty: Ty<'tcx>,
|
|
|
|
pub else_ty: Ty<'tcx>,
|
|
|
|
pub outer_span: Option<Span>,
|
2020-08-23 00:24:26 +00:00
|
|
|
pub opt_suggest_box_span: Option<Span>,
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 13:42:08 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
|
2020-01-22 07:51:01 +00:00
|
|
|
pub struct DerivedObligationCause<'tcx> {
|
2021-12-24 14:50:44 +00:00
|
|
|
/// The trait predicate of the parent obligation that led to the
|
2020-01-22 07:51:01 +00:00
|
|
|
/// current obligation. Note that only trait obligations lead to
|
2021-12-24 14:50:44 +00:00
|
|
|
/// derived obligations, so we just store the trait predicate here
|
2020-01-22 07:51:01 +00:00
|
|
|
/// directly.
|
2021-12-24 14:50:44 +00:00
|
|
|
pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// The parent trait had this cause.
|
2022-05-10 12:01:56 +00:00
|
|
|
pub parent_code: InternedObligationCauseCode<'tcx>,
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
|
2020-01-22 07:51:01 +00:00
|
|
|
pub enum SelectionError<'tcx> {
|
2021-10-01 13:05:17 +00:00
|
|
|
/// The trait is not implemented.
|
2020-01-22 07:51:01 +00:00
|
|
|
Unimplemented,
|
2021-10-01 13:05:17 +00:00
|
|
|
/// After a closure impl has selected, its "outputs" were evaluated
|
|
|
|
/// (which for closures includes the "input" type params) and they
|
|
|
|
/// didn't resolve. See `confirm_poly_trait_refs` for more.
|
2020-01-22 07:51:01 +00:00
|
|
|
OutputTypeParameterMismatch(
|
|
|
|
ty::PolyTraitRef<'tcx>,
|
|
|
|
ty::PolyTraitRef<'tcx>,
|
|
|
|
ty::error::TypeError<'tcx>,
|
|
|
|
),
|
2021-10-01 13:05:17 +00:00
|
|
|
/// The trait pointed by `DefId` is not object safe.
|
2020-01-22 07:51:01 +00:00
|
|
|
TraitNotObjectSafe(DefId),
|
2021-10-01 13:05:17 +00:00
|
|
|
/// A given constant couldn't be evaluated.
|
2021-03-02 15:47:06 +00:00
|
|
|
NotConstEvaluatable(NotConstEvaluatable),
|
2021-10-01 13:05:17 +00:00
|
|
|
/// Exceeded the recursion depth during type projection.
|
2022-02-26 03:55:07 +00:00
|
|
|
Overflow(OverflowError),
|
2021-10-01 13:05:17 +00:00
|
|
|
/// Signaling that an error has already been emitted, to avoid
|
|
|
|
/// multiple errors being shown.
|
2021-10-05 17:53:24 +00:00
|
|
|
ErrorReporting,
|
2021-10-01 13:05:17 +00:00
|
|
|
/// Multiple applicable `impl`s where found. The `DefId`s correspond to
|
|
|
|
/// all the `impl`s' Items.
|
2022-07-15 16:30:07 +00:00
|
|
|
Ambiguous(Vec<DefId>),
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// When performing resolution, it is typically the case that there
|
|
|
|
/// can be one of three outcomes:
|
|
|
|
///
|
|
|
|
/// - `Ok(Some(r))`: success occurred with result `r`
|
|
|
|
/// - `Ok(None)`: could not definitely determine anything, usually due
|
|
|
|
/// to inconclusive type inference.
|
|
|
|
/// - `Err(e)`: error `e` occurred
|
|
|
|
pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
/// Given the successful resolution of an obligation, the `ImplSource`
|
|
|
|
/// indicates where the impl comes from.
|
2020-01-22 07:51:01 +00:00
|
|
|
///
|
2020-05-11 15:25:33 +00:00
|
|
|
/// For example, the obligation may be satisfied by a specific impl (case A),
|
2020-01-22 07:51:01 +00:00
|
|
|
/// or it may be relative to some bound that is in scope (case B).
|
|
|
|
///
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ignore (illustrative)
|
2020-01-22 07:51:01 +00:00
|
|
|
/// impl<T:Clone> Clone<T> for Option<T> { ... } // Impl_1
|
|
|
|
/// impl<T:Clone> Clone<T> for Box<T> { ... } // Impl_2
|
2020-06-06 10:05:37 +00:00
|
|
|
/// impl Clone for i32 { ... } // Impl_3
|
2020-01-22 07:51:01 +00:00
|
|
|
///
|
2020-06-06 10:05:37 +00:00
|
|
|
/// fn foo<T: Clone>(concrete: Option<Box<i32>>, param: T, mixed: Option<T>) {
|
2021-07-09 16:26:28 +00:00
|
|
|
/// // Case A: ImplSource points at a specific impl. Only possible when
|
2020-06-06 10:05:37 +00:00
|
|
|
/// // type is concretely known. If the impl itself has bounded
|
2021-07-09 16:26:28 +00:00
|
|
|
/// // type parameters, ImplSource will carry resolutions for those as well:
|
2022-03-30 19:14:15 +00:00
|
|
|
/// concrete.clone(); // ImplSource(Impl_1, [ImplSource(Impl_2, [ImplSource(Impl_3)])])
|
2020-01-22 07:51:01 +00:00
|
|
|
///
|
2020-06-06 10:05:37 +00:00
|
|
|
/// // Case A: ImplSource points at a specific impl. Only possible when
|
|
|
|
/// // type is concretely known. If the impl itself has bounded
|
|
|
|
/// // type parameters, ImplSource will carry resolutions for those as well:
|
|
|
|
/// concrete.clone(); // ImplSource(Impl_1, [ImplSource(Impl_2, [ImplSource(Impl_3)])])
|
2020-01-22 07:51:01 +00:00
|
|
|
///
|
2020-06-06 10:05:37 +00:00
|
|
|
/// // Case B: ImplSource must be provided by caller. This applies when
|
|
|
|
/// // type is a type parameter.
|
2020-09-24 17:22:36 +00:00
|
|
|
/// param.clone(); // ImplSource::Param
|
2020-01-22 07:51:01 +00:00
|
|
|
///
|
2020-06-06 10:05:37 +00:00
|
|
|
/// // Case C: A mix of cases A and B.
|
2020-09-24 17:22:36 +00:00
|
|
|
/// mixed.clone(); // ImplSource(Impl_1, [ImplSource::Param])
|
2020-01-22 07:51:01 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### The type parameter `N`
|
|
|
|
///
|
2020-06-02 15:54:24 +00:00
|
|
|
/// See explanation on `ImplSourceUserDefinedData`.
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub enum ImplSource<'tcx, N> {
|
|
|
|
/// ImplSource identifying a particular impl.
|
2020-09-24 17:22:36 +00:00
|
|
|
UserDefined(ImplSourceUserDefinedData<'tcx, N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
/// ImplSource for auto trait implementations.
|
2020-01-22 07:51:01 +00:00
|
|
|
/// This carries the information and nested obligations with regards
|
|
|
|
/// to an auto implementation for a trait `Trait`. The nested obligations
|
|
|
|
/// ensure the trait implementation holds for all the constituent types.
|
2020-09-24 17:22:36 +00:00
|
|
|
AutoImpl(ImplSourceAutoImplData<N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Successful resolution to an obligation provided by the caller
|
|
|
|
/// for some type parameter. The `Vec<N>` represents the
|
|
|
|
/// obligations incurred from normalizing the where-clause (if
|
|
|
|
/// any).
|
2021-08-27 05:02:23 +00:00
|
|
|
Param(Vec<N>, ty::BoundConstness),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Virtual calls through an object.
|
2020-09-24 17:22:36 +00:00
|
|
|
Object(ImplSourceObjectData<'tcx, N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Successful resolution for a builtin trait.
|
2020-09-24 17:22:36 +00:00
|
|
|
Builtin(ImplSourceBuiltinData<N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2021-08-18 04:45:18 +00:00
|
|
|
/// ImplSource for trait upcasting coercion
|
|
|
|
TraitUpcasting(ImplSourceTraitUpcastingData<'tcx, N>),
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
/// ImplSource automatically generated for a closure. The `DefId` is the ID
|
2021-08-22 12:46:15 +00:00
|
|
|
/// of the closure expression. This is an `ImplSource::UserDefined` in spirit, but the
|
2020-01-22 07:51:01 +00:00
|
|
|
/// impl is generated by the compiler and does not appear in the source.
|
2020-09-24 17:22:36 +00:00
|
|
|
Closure(ImplSourceClosureData<'tcx, N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
|
|
|
/// Same as above, but for a function pointer type with the given signature.
|
2020-09-24 17:22:36 +00:00
|
|
|
FnPointer(ImplSourceFnPointerData<'tcx, N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
/// ImplSource for a builtin `DeterminantKind` trait implementation.
|
2020-09-24 17:22:36 +00:00
|
|
|
DiscriminantKind(ImplSourceDiscriminantKindData),
|
2020-04-05 17:57:32 +00:00
|
|
|
|
2020-10-19 13:38:11 +00:00
|
|
|
/// ImplSource for a builtin `Pointee` trait implementation.
|
|
|
|
Pointee(ImplSourcePointeeData),
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
/// ImplSource automatically generated for a generator.
|
2020-09-24 17:22:36 +00:00
|
|
|
Generator(ImplSourceGeneratorData<'tcx, N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
/// ImplSource for a trait alias.
|
2020-09-24 17:22:36 +00:00
|
|
|
TraitAlias(ImplSourceTraitAliasData<'tcx, N>),
|
2021-09-01 16:34:28 +00:00
|
|
|
|
|
|
|
/// ImplSource for a `const Drop` implementation.
|
2022-03-21 05:52:41 +00:00
|
|
|
ConstDestruct(ImplSourceConstDestructData<N>),
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
impl<'tcx, N> ImplSource<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub fn nested_obligations(self) -> Vec<N> {
|
|
|
|
match self {
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::UserDefined(i) => i.nested,
|
2020-11-22 01:13:53 +00:00
|
|
|
ImplSource::Param(n, _) => n,
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::Builtin(i) => i.nested,
|
|
|
|
ImplSource::AutoImpl(d) => d.nested,
|
|
|
|
ImplSource::Closure(c) => c.nested,
|
|
|
|
ImplSource::Generator(c) => c.nested,
|
|
|
|
ImplSource::Object(d) => d.nested,
|
|
|
|
ImplSource::FnPointer(d) => d.nested,
|
2020-10-19 13:38:11 +00:00
|
|
|
ImplSource::DiscriminantKind(ImplSourceDiscriminantKindData)
|
2022-01-18 09:43:49 +00:00
|
|
|
| ImplSource::Pointee(ImplSourcePointeeData) => Vec::new(),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::TraitAlias(d) => d.nested,
|
2021-08-18 04:45:18 +00:00
|
|
|
ImplSource::TraitUpcasting(d) => d.nested,
|
2022-03-21 05:52:41 +00:00
|
|
|
ImplSource::ConstDestruct(i) => i.nested,
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 00:35:47 +00:00
|
|
|
pub fn borrow_nested_obligations(&self) -> &[N] {
|
|
|
|
match &self {
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::UserDefined(i) => &i.nested[..],
|
2021-12-03 02:06:36 +00:00
|
|
|
ImplSource::Param(n, _) => &n,
|
|
|
|
ImplSource::Builtin(i) => &i.nested,
|
|
|
|
ImplSource::AutoImpl(d) => &d.nested,
|
|
|
|
ImplSource::Closure(c) => &c.nested,
|
|
|
|
ImplSource::Generator(c) => &c.nested,
|
|
|
|
ImplSource::Object(d) => &d.nested,
|
|
|
|
ImplSource::FnPointer(d) => &d.nested,
|
2020-10-19 13:38:11 +00:00
|
|
|
ImplSource::DiscriminantKind(ImplSourceDiscriminantKindData)
|
2022-01-18 09:43:49 +00:00
|
|
|
| ImplSource::Pointee(ImplSourcePointeeData) => &[],
|
2021-12-03 02:06:36 +00:00
|
|
|
ImplSource::TraitAlias(d) => &d.nested,
|
|
|
|
ImplSource::TraitUpcasting(d) => &d.nested,
|
2022-03-21 05:52:41 +00:00
|
|
|
ImplSource::ConstDestruct(i) => &i.nested,
|
2020-02-19 00:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
|
2020-01-22 07:51:01 +00:00
|
|
|
where
|
|
|
|
F: FnMut(N) -> M,
|
|
|
|
{
|
|
|
|
match self {
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData {
|
2020-01-22 07:51:01 +00:00
|
|
|
impl_def_id: i.impl_def_id,
|
|
|
|
substs: i.substs,
|
|
|
|
nested: i.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-11-22 01:13:53 +00:00
|
|
|
ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::Builtin(i) => ImplSource::Builtin(ImplSourceBuiltinData {
|
2020-05-11 15:25:33 +00:00
|
|
|
nested: i.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::Object(o) => ImplSource::Object(ImplSourceObjectData {
|
2020-01-22 07:51:01 +00:00
|
|
|
upcast_trait_ref: o.upcast_trait_ref,
|
|
|
|
vtable_base: o.vtable_base,
|
|
|
|
nested: o.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::AutoImpl(d) => ImplSource::AutoImpl(ImplSourceAutoImplData {
|
2020-01-22 07:51:01 +00:00
|
|
|
trait_def_id: d.trait_def_id,
|
|
|
|
nested: d.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::Closure(c) => ImplSource::Closure(ImplSourceClosureData {
|
2020-01-22 07:51:01 +00:00
|
|
|
closure_def_id: c.closure_def_id,
|
|
|
|
substs: c.substs,
|
|
|
|
nested: c.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::Generator(c) => ImplSource::Generator(ImplSourceGeneratorData {
|
2020-01-22 07:51:01 +00:00
|
|
|
generator_def_id: c.generator_def_id,
|
|
|
|
substs: c.substs,
|
|
|
|
nested: c.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::FnPointer(p) => ImplSource::FnPointer(ImplSourceFnPointerData {
|
2020-01-22 07:51:01 +00:00
|
|
|
fn_ty: p.fn_ty,
|
|
|
|
nested: p.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::DiscriminantKind(ImplSourceDiscriminantKindData) => {
|
|
|
|
ImplSource::DiscriminantKind(ImplSourceDiscriminantKindData)
|
2020-04-05 17:57:32 +00:00
|
|
|
}
|
2020-10-19 13:38:11 +00:00
|
|
|
ImplSource::Pointee(ImplSourcePointeeData) => {
|
|
|
|
ImplSource::Pointee(ImplSourcePointeeData)
|
|
|
|
}
|
2020-09-24 17:22:36 +00:00
|
|
|
ImplSource::TraitAlias(d) => ImplSource::TraitAlias(ImplSourceTraitAliasData {
|
2020-01-22 07:51:01 +00:00
|
|
|
alias_def_id: d.alias_def_id,
|
|
|
|
substs: d.substs,
|
|
|
|
nested: d.nested.into_iter().map(f).collect(),
|
|
|
|
}),
|
2021-08-18 04:45:18 +00:00
|
|
|
ImplSource::TraitUpcasting(d) => {
|
|
|
|
ImplSource::TraitUpcasting(ImplSourceTraitUpcastingData {
|
|
|
|
upcast_trait_ref: d.upcast_trait_ref,
|
|
|
|
vtable_vptr_slot: d.vtable_vptr_slot,
|
|
|
|
nested: d.nested.into_iter().map(f).collect(),
|
|
|
|
})
|
|
|
|
}
|
2022-03-21 05:52:41 +00:00
|
|
|
ImplSource::ConstDestruct(i) => {
|
|
|
|
ImplSource::ConstDestruct(ImplSourceConstDestructData {
|
|
|
|
nested: i.nested.into_iter().map(f).collect(),
|
|
|
|
})
|
|
|
|
}
|
2020-01-22 07:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Identifies a particular impl in the source, along with a set of
|
|
|
|
/// substitutions from the impl's type/lifetime parameters. The
|
|
|
|
/// `nested` vector corresponds to the nested obligations attached to
|
|
|
|
/// the impl's type parameters.
|
|
|
|
///
|
|
|
|
/// The type parameter `N` indicates the type used for "nested
|
|
|
|
/// obligations" that are required by the impl. During type-check, this
|
|
|
|
/// is `Obligation`, as one might expect. During codegen, however, this
|
|
|
|
/// is `()`, because codegen only requires a shallow resolution of an
|
|
|
|
/// impl, and nested obligations are satisfied later.
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-06-02 15:54:24 +00:00
|
|
|
pub struct ImplSourceUserDefinedData<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub impl_def_id: DefId,
|
|
|
|
pub substs: SubstsRef<'tcx>,
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceGeneratorData<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub generator_def_id: DefId,
|
|
|
|
pub substs: SubstsRef<'tcx>,
|
|
|
|
/// Nested obligations. This can be non-empty if the generator
|
|
|
|
/// signature contains associated types.
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceClosureData<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub closure_def_id: DefId,
|
|
|
|
pub substs: SubstsRef<'tcx>,
|
|
|
|
/// Nested obligations. This can be non-empty if the closure
|
|
|
|
/// signature contains associated types.
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceAutoImplData<N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub trait_def_id: DefId,
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2021-08-18 04:45:18 +00:00
|
|
|
pub struct ImplSourceTraitUpcastingData<'tcx, N> {
|
|
|
|
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
|
|
|
|
pub upcast_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
|
|
|
|
/// The vtable is formed by concatenating together the method lists of
|
|
|
|
/// the base object trait and all supertraits, pointers to supertrait vtable will
|
|
|
|
/// be provided when necessary; this is the position of `upcast_trait_ref`'s vtable
|
|
|
|
/// within that vtable.
|
|
|
|
pub vtable_vptr_slot: Option<usize>,
|
|
|
|
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceBuiltinData<N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceObjectData<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
|
|
|
|
pub upcast_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
|
|
|
|
/// The vtable is formed by concatenating together the method lists of
|
2021-08-18 04:45:18 +00:00
|
|
|
/// the base object trait and all supertraits, pointers to supertrait vtable will
|
|
|
|
/// be provided when necessary; this is the start of `upcast_trait_ref`'s methods
|
|
|
|
/// in that vtable.
|
2020-01-22 07:51:01 +00:00
|
|
|
pub vtable_base: usize,
|
|
|
|
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceFnPointerData<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub fn_ty: Ty<'tcx>,
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:57:32 +00:00
|
|
|
// FIXME(@lcnr): This should be refactored and merged with other builtin vtables.
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceDiscriminantKindData;
|
2020-04-05 17:57:32 +00:00
|
|
|
|
2020-10-19 13:38:11 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
|
|
|
|
pub struct ImplSourcePointeeData;
|
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2022-03-21 05:52:41 +00:00
|
|
|
pub struct ImplSourceConstDestructData<N> {
|
2022-01-18 09:43:49 +00:00
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
2021-09-01 16:34:28 +00:00
|
|
|
|
2022-06-17 09:53:29 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
|
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub struct ImplSourceTraitAliasData<'tcx, N> {
|
2020-01-22 07:51:01 +00:00
|
|
|
pub alias_def_id: DefId,
|
|
|
|
pub substs: SubstsRef<'tcx>,
|
|
|
|
pub nested: Vec<N>,
|
|
|
|
}
|
|
|
|
|
2021-09-12 18:49:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
|
2020-02-10 18:55:49 +00:00
|
|
|
pub enum ObjectSafetyViolation {
|
|
|
|
/// `Self: Sized` declared on the trait.
|
|
|
|
SizedSelf(SmallVec<[Span; 1]>),
|
|
|
|
|
|
|
|
/// Supertrait reference references `Self` an in illegal location
|
|
|
|
/// (e.g., `trait Foo : Bar<Self>`).
|
|
|
|
SupertraitSelf(SmallVec<[Span; 1]>),
|
|
|
|
|
|
|
|
/// Method has something illegal.
|
2020-04-19 11:00:18 +00:00
|
|
|
Method(Symbol, MethodViolationCode, Span),
|
2020-02-10 18:55:49 +00:00
|
|
|
|
|
|
|
/// Associated const.
|
2020-04-19 11:00:18 +00:00
|
|
|
AssocConst(Symbol, Span),
|
2021-04-27 18:34:23 +00:00
|
|
|
|
|
|
|
/// GAT
|
|
|
|
GAT(Symbol, Span),
|
2020-02-10 18:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectSafetyViolation {
|
|
|
|
pub fn error_msg(&self) -> Cow<'static, str> {
|
2022-06-25 21:59:45 +00:00
|
|
|
match self {
|
2020-02-10 18:55:49 +00:00
|
|
|
ObjectSafetyViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
|
|
|
|
ObjectSafetyViolation::SupertraitSelf(ref spans) => {
|
|
|
|
if spans.iter().any(|sp| *sp != DUMMY_SP) {
|
2020-10-20 00:57:18 +00:00
|
|
|
"it uses `Self` as a type parameter".into()
|
2020-02-10 18:55:49 +00:00
|
|
|
} else {
|
|
|
|
"it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 21:59:45 +00:00
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => {
|
2020-02-10 18:55:49 +00:00
|
|
|
format!("associated function `{}` has no `self` parameter", name).into()
|
|
|
|
}
|
|
|
|
ObjectSafetyViolation::Method(
|
|
|
|
name,
|
|
|
|
MethodViolationCode::ReferencesSelfInput(_),
|
|
|
|
DUMMY_SP,
|
|
|
|
) => format!("method `{}` references the `Self` type in its parameters", name).into(),
|
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelfInput(_), _) => {
|
|
|
|
format!("method `{}` references the `Self` type in this parameter", name).into()
|
|
|
|
}
|
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelfOutput, _) => {
|
|
|
|
format!("method `{}` references the `Self` type in its return type", name).into()
|
|
|
|
}
|
2022-09-11 09:13:55 +00:00
|
|
|
ObjectSafetyViolation::Method(
|
|
|
|
name,
|
|
|
|
MethodViolationCode::ReferencesImplTraitInTrait,
|
|
|
|
_,
|
|
|
|
) => format!("method `{}` references an `impl Trait` type in its return type", name)
|
|
|
|
.into(),
|
2020-02-10 18:55:49 +00:00
|
|
|
ObjectSafetyViolation::Method(
|
|
|
|
name,
|
|
|
|
MethodViolationCode::WhereClauseReferencesSelf,
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
format!("method `{}` references the `Self` type in its `where` clause", name).into()
|
|
|
|
}
|
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::Generic, _) => {
|
|
|
|
format!("method `{}` has generic type parameters", name).into()
|
|
|
|
}
|
2022-06-25 21:59:45 +00:00
|
|
|
ObjectSafetyViolation::Method(
|
|
|
|
name,
|
|
|
|
MethodViolationCode::UndispatchableReceiver(_),
|
|
|
|
_,
|
|
|
|
) => format!("method `{}`'s `self` parameter cannot be dispatched on", name).into(),
|
2020-02-10 18:55:49 +00:00
|
|
|
ObjectSafetyViolation::AssocConst(name, DUMMY_SP) => {
|
|
|
|
format!("it contains associated `const` `{}`", name).into()
|
|
|
|
}
|
|
|
|
ObjectSafetyViolation::AssocConst(..) => "it contains this associated `const`".into(),
|
2021-04-27 18:34:23 +00:00
|
|
|
ObjectSafetyViolation::GAT(name, _) => {
|
|
|
|
format!("it contains the generic associated type `{}`", name).into()
|
|
|
|
}
|
2020-02-10 18:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:41:46 +00:00
|
|
|
pub fn solution(&self, err: &mut Diagnostic) {
|
2022-06-25 21:59:45 +00:00
|
|
|
match self {
|
2020-10-16 00:23:45 +00:00
|
|
|
ObjectSafetyViolation::SizedSelf(_) | ObjectSafetyViolation::SupertraitSelf(_) => {}
|
|
|
|
ObjectSafetyViolation::Method(
|
|
|
|
name,
|
2022-06-25 21:59:45 +00:00
|
|
|
MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
|
2020-10-16 00:23:45 +00:00
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
err.span_suggestion(
|
2022-06-25 21:59:45 +00:00
|
|
|
add_self_sugg.1,
|
|
|
|
format!(
|
2020-10-16 00:23:45 +00:00
|
|
|
"consider turning `{}` into a method by giving it a `&self` argument",
|
|
|
|
name
|
|
|
|
),
|
2022-06-25 21:59:45 +00:00
|
|
|
add_self_sugg.0.to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
err.span_suggestion(
|
|
|
|
make_sized_sugg.1,
|
|
|
|
format!(
|
|
|
|
"alternatively, consider constraining `{}` so it does not apply to \
|
|
|
|
trait objects",
|
|
|
|
name
|
|
|
|
),
|
|
|
|
make_sized_sugg.0.to_string(),
|
2020-10-16 00:23:45 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2020-02-10 18:55:49 +00:00
|
|
|
}
|
|
|
|
ObjectSafetyViolation::Method(
|
|
|
|
name,
|
2022-06-25 21:59:45 +00:00
|
|
|
MethodViolationCode::UndispatchableReceiver(Some(span)),
|
|
|
|
_,
|
2020-10-16 00:23:45 +00:00
|
|
|
) => {
|
|
|
|
err.span_suggestion(
|
2022-06-25 21:59:45 +00:00
|
|
|
*span,
|
2020-10-16 00:23:45 +00:00
|
|
|
&format!(
|
|
|
|
"consider changing method `{}`'s `self` parameter to be `&self`",
|
|
|
|
name
|
|
|
|
),
|
2022-06-13 06:48:40 +00:00
|
|
|
"&Self",
|
2020-10-16 00:23:45 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2020-02-10 18:55:49 +00:00
|
|
|
ObjectSafetyViolation::AssocConst(name, _)
|
2021-04-27 18:34:23 +00:00
|
|
|
| ObjectSafetyViolation::GAT(name, _)
|
2020-02-10 18:55:49 +00:00
|
|
|
| ObjectSafetyViolation::Method(name, ..) => {
|
2020-10-16 00:23:45 +00:00
|
|
|
err.help(&format!("consider moving `{}` to another trait", name));
|
2020-02-10 18:55:49 +00:00
|
|
|
}
|
2020-10-16 00:23:45 +00:00
|
|
|
}
|
2020-02-10 18:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spans(&self) -> SmallVec<[Span; 1]> {
|
|
|
|
// When `span` comes from a separate crate, it'll be `DUMMY_SP`. Treat it as `None` so
|
|
|
|
// diagnostics use a `note` instead of a `span_label`.
|
|
|
|
match self {
|
|
|
|
ObjectSafetyViolation::SupertraitSelf(spans)
|
|
|
|
| ObjectSafetyViolation::SizedSelf(spans) => spans.clone(),
|
|
|
|
ObjectSafetyViolation::AssocConst(_, span)
|
2021-04-27 18:34:23 +00:00
|
|
|
| ObjectSafetyViolation::GAT(_, span)
|
2020-02-10 18:55:49 +00:00
|
|
|
| ObjectSafetyViolation::Method(_, _, span)
|
|
|
|
if *span != DUMMY_SP =>
|
|
|
|
{
|
|
|
|
smallvec![*span]
|
|
|
|
}
|
|
|
|
_ => smallvec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reasons a method might not be object-safe.
|
2022-06-25 21:59:45 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
|
2020-02-10 18:55:49 +00:00
|
|
|
pub enum MethodViolationCode {
|
|
|
|
/// e.g., `fn foo()`
|
2022-06-25 21:59:45 +00:00
|
|
|
StaticMethod(Option<(/* add &self */ (String, Span), /* add Self: Sized */ (String, Span))>),
|
2020-02-10 18:55:49 +00:00
|
|
|
|
|
|
|
/// e.g., `fn foo(&self, x: Self)`
|
2022-06-25 21:59:45 +00:00
|
|
|
ReferencesSelfInput(Option<Span>),
|
2020-02-10 18:55:49 +00:00
|
|
|
|
|
|
|
/// e.g., `fn foo(&self) -> Self`
|
|
|
|
ReferencesSelfOutput,
|
|
|
|
|
2022-09-11 09:13:55 +00:00
|
|
|
/// e.g., `fn foo(&self) -> impl Sized`
|
|
|
|
ReferencesImplTraitInTrait,
|
|
|
|
|
2020-02-10 18:55:49 +00:00
|
|
|
/// e.g., `fn foo(&self) where Self: Clone`
|
|
|
|
WhereClauseReferencesSelf,
|
|
|
|
|
|
|
|
/// e.g., `fn foo<A>()`
|
|
|
|
Generic,
|
|
|
|
|
|
|
|
/// the method's receiver (`self` argument) can't be dispatched on
|
2022-06-25 21:59:45 +00:00
|
|
|
UndispatchableReceiver(Option<Span>),
|
2020-02-10 18:55:49 +00:00
|
|
|
}
|
2022-05-01 09:03:14 +00:00
|
|
|
|
2022-09-09 11:36:27 +00:00
|
|
|
/// These are the error cases for `codegen_select_candidate`.
|
2022-05-01 09:03:14 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, HashStable, Encodable, Decodable)]
|
|
|
|
pub enum CodegenObligationError {
|
|
|
|
/// Ambiguity can happen when monomorphizing during trans
|
|
|
|
/// expands to some humongous type that never occurred
|
|
|
|
/// statically -- this humongous type can then overflow,
|
|
|
|
/// leading to an ambiguous result. So report this as an
|
|
|
|
/// overflow bug, since I believe this is the only case
|
|
|
|
/// where ambiguity can result.
|
|
|
|
Ambiguity,
|
|
|
|
/// This can trigger when we probe for the source of a `'static` lifetime requirement
|
|
|
|
/// on a trait object: `impl Foo for dyn Trait {}` has an implicit `'static` bound.
|
|
|
|
/// This can also trigger when we have a global bound that is not actually satisfied,
|
|
|
|
/// but was included during typeck due to the trivial_bounds feature.
|
|
|
|
Unimplemented,
|
|
|
|
FulfillmentError,
|
|
|
|
}
|