2020-03-05 18:07:42 -03:00
|
|
|
//! Trait Resolution. See the [rustc dev guide] for more information on how this works.
|
2020-01-22 08:51:01 +01:00
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2020-01-22 09:04:50 +01:00
|
|
|
pub mod query;
|
2020-01-22 09:01:22 +01:00
|
|
|
pub mod select;
|
2023-02-03 02:29:52 +00:00
|
|
|
pub mod solve;
|
2020-01-22 13:44:59 +01:00
|
|
|
pub mod specialization_graph;
|
2020-01-22 13:42:04 +01:00
|
|
|
mod structural_impls;
|
2020-01-22 09:01:22 +01:00
|
|
|
|
2022-09-16 17:00:11 -04:00
|
|
|
use std::borrow::Cow;
|
2023-09-14 12:05:05 +10:00
|
|
|
use std::hash::{Hash, Hasher};
|
2020-01-22 08:51:01 +01: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 16:55:39 -04:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2024-02-23 10:20:45 +11:00
|
|
|
use rustc_errors::{Applicability, Diag, EmissionGuarantee};
|
2020-01-22 08:51:01 +01:00
|
|
|
use rustc_hir as hir;
|
2024-03-06 17:24:13 +11:00
|
|
|
use rustc_hir::HirId;
|
2023-01-15 12:58:46 +01:00
|
|
|
use rustc_hir::def_id::DefId;
|
2024-04-29 11:14:55 +10:00
|
|
|
use rustc_macros::{
|
|
|
|
|
Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
|
|
|
|
|
};
|
2023-01-15 12:58:46 +01:00
|
|
|
use rustc_span::def_id::{CRATE_DEF_ID, LocalDefId};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::Symbol;
|
2020-01-22 08:51:01 +01:00
|
|
|
use rustc_span::{DUMMY_SP, Span};
|
2024-05-17 12:16:36 -04:00
|
|
|
// FIXME: Remove this import and import via `solve::`
|
2024-11-20 11:31:49 +01:00
|
|
|
pub use rustc_type_ir::solve::BuiltinImplSource;
|
2024-04-29 16:18:37 +10:00
|
|
|
use smallvec::{SmallVec, smallvec};
|
2024-10-10 19:56:21 +01:00
|
|
|
use thin_vec::ThinVec;
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2020-01-22 09:01:22 +01:00
|
|
|
pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache};
|
2024-05-17 12:16:36 -04:00
|
|
|
use crate::mir::ConstraintCategory;
|
2024-06-14 13:57:56 -04:00
|
|
|
use crate::ty::abstract_const::NotConstEvaluatable;
|
|
|
|
|
use crate::ty::{self, AdtKind, GenericArgsRef, Ty};
|
2020-01-22 08:54:46 +01:00
|
|
|
|
2020-01-22 08:51:01 +01:00
|
|
|
/// The reason why we incurred this obligation; used for error reporting.
|
2020-06-03 23:59:30 +02:00
|
|
|
///
|
2021-11-11 12:01:12 +11: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 23:59:30 +02: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.
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
2021-11-11 12:01:12 +11:00
|
|
|
pub struct ObligationCause<'tcx> {
|
2020-01-22 08:51:01 +01: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.
|
2023-01-15 12:58:46 +01:00
|
|
|
pub body_id: LocalDefId,
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2022-05-10 12:01:56 +00:00
|
|
|
code: InternedObligationCauseCode<'tcx>,
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-11 12:01:12 +11: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 23:49:57 +01:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.body_id.hash(state);
|
|
|
|
|
self.span.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 08:51:01 +01:00
|
|
|
impl<'tcx> ObligationCause<'tcx> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new(
|
|
|
|
|
span: Span,
|
2023-01-15 12:58:46 +01:00
|
|
|
body_id: LocalDefId,
|
2020-01-22 08:51:01 +01:00
|
|
|
code: ObligationCauseCode<'tcx>,
|
|
|
|
|
) -> ObligationCause<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
ObligationCause { span, body_id, code: code.into() }
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-15 12:58:46 +01:00
|
|
|
pub fn misc(span: Span, body_id: LocalDefId) -> ObligationCause<'tcx> {
|
2024-05-09 20:12:47 -04:00
|
|
|
ObligationCause::new(span, body_id, ObligationCauseCode::Misc)
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-03 23:59:30 +02:00
|
|
|
#[inline(always)]
|
2020-01-22 08:51:01 +01:00
|
|
|
pub fn dummy() -> ObligationCause<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
ObligationCause::dummy_with_span(DUMMY_SP)
|
2021-11-11 12:01:12 +11:00
|
|
|
}
|
|
|
|
|
|
2022-05-12 11:29:01 +00:00
|
|
|
#[inline(always)]
|
2021-11-11 12:01:12 +11:00
|
|
|
pub fn dummy_with_span(span: Span) -> ObligationCause<'tcx> {
|
2023-01-15 12:58:46 +01:00
|
|
|
ObligationCause { span, body_id: CRATE_DEF_ID, code: Default::default() }
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-11 12:01:12 +11:00
|
|
|
#[inline]
|
|
|
|
|
pub fn code(&self) -> &ObligationCauseCode<'tcx> {
|
2022-05-10 12:01:56 +00:00
|
|
|
&self.code
|
2021-11-11 12:01:12 +11: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>,
|
2024-05-09 20:25:11 -04:00
|
|
|
variant: impl FnOnce(DerivedCause<'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
|
2023-12-19 12:39:58 -05:00
|
|
|
// by using -Z verbose-internals or just a CLI argument.
|
2024-05-09 20:25:11 -04:00
|
|
|
self.code = variant(DerivedCause { parent_trait_pred, parent_code: self.code }).into();
|
2022-05-10 09:26:09 +00:00
|
|
|
self
|
2021-11-11 12:01:12 +11:00
|
|
|
}
|
2022-09-16 17:00:11 -04:00
|
|
|
|
2022-10-27 16:15:11 +00:00
|
|
|
pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
|
2022-09-16 17:00:11 -04:00
|
|
|
match self.code() {
|
2024-05-09 20:05:59 -04:00
|
|
|
ObligationCauseCode::MatchImpl(cause, _) => cause.to_constraint_category(),
|
|
|
|
|
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span) => {
|
2022-09-16 17:20:11 -04:00
|
|
|
ConstraintCategory::Predicate(*predicate_span)
|
|
|
|
|
}
|
2022-09-16 17:00:11 -04:00
|
|
|
_ => ConstraintCategory::BoringNoLocation,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
2020-06-30 17:41:15 -07:00
|
|
|
pub struct UnifyReceiverContext<'tcx> {
|
|
|
|
|
pub assoc_item: ty::AssocItem,
|
|
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
2023-07-11 22:35:29 +01:00
|
|
|
pub args: GenericArgsRef<'tcx>,
|
2020-06-30 17:41:15 -07:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, PartialEq, Eq, Default, HashStable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
|
2022-05-10 11:26:53 +00:00
|
|
|
pub struct InternedObligationCauseCode<'tcx> {
|
2024-05-09 20:12:47 -04:00
|
|
|
/// `None` for `ObligationCauseCode::Misc` (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-11-03 18:52:08 +00:00
|
|
|
impl<'tcx> std::fmt::Debug for InternedObligationCauseCode<'tcx> {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
let cause: &ObligationCauseCode<'_> = self;
|
|
|
|
|
cause.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2024-05-09 20:12:47 -04:00
|
|
|
code: if let ObligationCauseCode::Misc = 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 {
|
2024-05-09 20:12:47 -04:00
|
|
|
self.code.as_deref().unwrap_or(&ObligationCauseCode::Misc)
|
2022-05-10 11:26:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
2020-01-22 08:51:01 +01:00
|
|
|
pub enum ObligationCauseCode<'tcx> {
|
|
|
|
|
/// Not well classified or should be obvious from the span.
|
2024-05-09 20:12:47 -04:00
|
|
|
Misc,
|
2020-01-22 08:51:01 +01:00
|
|
|
|
|
|
|
|
/// A slice or array is WF only if `T: Sized`.
|
|
|
|
|
SliceOrArrayElem,
|
|
|
|
|
|
|
|
|
|
/// A tuple is WF only if its middle elements are `Sized`.
|
|
|
|
|
TupleElem,
|
|
|
|
|
|
2024-05-10 11:04:53 -04:00
|
|
|
/// Represents a clause that comes from a specific item.
|
|
|
|
|
/// The span corresponds to the clause.
|
|
|
|
|
WhereClause(DefId, Span),
|
|
|
|
|
|
2024-11-08 04:27:20 +00:00
|
|
|
/// Represents a bound for an opaque we are checking the well-formedness of.
|
|
|
|
|
/// The def-id corresponds to a specific definition site that we found the
|
|
|
|
|
/// hidden type from, if any.
|
|
|
|
|
OpaqueTypeBound(Span, Option<LocalDefId>),
|
|
|
|
|
|
2024-05-10 11:04:53 -04:00
|
|
|
/// Like `WhereClause`, but also identifies the expression
|
|
|
|
|
/// which requires the `where` clause to be proven, and also
|
|
|
|
|
/// identifies the index of the predicate in the `predicates_of`
|
|
|
|
|
/// list of the item.
|
|
|
|
|
WhereClauseInExpr(DefId, Span, HirId, usize),
|
2022-08-16 06:27:22 +00:00
|
|
|
|
2020-01-22 08:51:01 +01: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 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`.
|
2024-03-06 17:24:13 +11:00
|
|
|
VariableType(HirId),
|
2020-01-22 08:51:01 +01:00
|
|
|
/// Argument type must be `Sized`.
|
2024-03-06 17:24:13 +11:00
|
|
|
SizedArgumentType(Option<HirId>),
|
2020-01-22 08:51:01 +01:00
|
|
|
/// Return type must be `Sized`.
|
|
|
|
|
SizedReturnType,
|
2024-01-24 02:52:29 +00:00
|
|
|
/// Return type of a call expression must be `Sized`.
|
|
|
|
|
SizedCallReturnType,
|
2020-01-22 08:51:01 +01:00
|
|
|
/// Yield type must be `Sized`.
|
|
|
|
|
SizedYieldType,
|
2020-02-13 11:00:55 +00:00
|
|
|
/// Inline asm operand type must be `Sized`.
|
|
|
|
|
InlineAsmSized,
|
2023-09-23 04:03:24 +00:00
|
|
|
/// Captured closure type must be `Sized`.
|
2023-09-23 00:26:26 +00:00
|
|
|
SizedClosureCapture(LocalDefId),
|
2023-10-19 21:46:28 +00:00
|
|
|
/// Types live across coroutine yields must be `Sized`.
|
2023-10-19 16:06:43 +00:00
|
|
|
SizedCoroutineInterior(LocalDefId),
|
2022-03-31 12:50:53 +02:00
|
|
|
/// `[expr; N]` requires `type_of(expr): Copy`.
|
|
|
|
|
RepeatElementCopy {
|
2023-07-21 15:59:28 +00:00
|
|
|
/// If element is a `const fn` or const ctor we display a help message suggesting
|
|
|
|
|
/// to move it to a new `const` item while saying that `T` doesn't implement `Copy`.
|
|
|
|
|
is_constable: IsConstable,
|
|
|
|
|
elt_type: Ty<'tcx>,
|
|
|
|
|
elt_span: Span,
|
|
|
|
|
/// Span of the statement/item in which the repeat expression occurs. We can use this to
|
|
|
|
|
/// place a `const` declaration before it
|
|
|
|
|
elt_stmt_span: Span,
|
2022-03-31 12:50:53 +02:00
|
|
|
},
|
2020-01-22 08:51:01 +01: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 15:13:49 -07:00
|
|
|
span: Span,
|
2020-01-22 08:51:01 +01:00
|
|
|
last: bool,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// Constant expressions must be sized.
|
|
|
|
|
ConstSized,
|
|
|
|
|
|
|
|
|
|
/// `static` items must have `Sync` type.
|
|
|
|
|
SharedStatic,
|
|
|
|
|
|
2024-04-25 16:55:15 -04:00
|
|
|
/// Derived obligation (i.e. theoretical `where` clause) on a built-in
|
|
|
|
|
/// implementation like `Copy` or `Sized`.
|
2024-05-09 20:25:11 -04:00
|
|
|
BuiltinDerived(DerivedCause<'tcx>),
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2024-04-25 16:55:15 -04:00
|
|
|
/// Derived obligation (i.e. `where` clause) on an user-provided impl
|
|
|
|
|
/// or a trait alias.
|
2024-05-09 20:25:11 -04:00
|
|
|
ImplDerived(Box<ImplDerivedCause<'tcx>>),
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2024-04-25 16:55:15 -04:00
|
|
|
/// Derived obligation for WF goals.
|
2024-05-09 20:25:11 -04:00
|
|
|
WellFormedDerived(DerivedCause<'tcx>),
|
2020-04-11 14:14:16 -07:00
|
|
|
|
2024-05-09 20:25:11 -04:00
|
|
|
/// Derived obligation refined to point at a specific argument in
|
|
|
|
|
/// a call or method expression.
|
|
|
|
|
FunctionArg {
|
2021-09-07 11:19:57 +00:00
|
|
|
/// The node of the relevant argument in the function call.
|
2024-03-06 17:24:13 +11:00
|
|
|
arg_hir_id: HirId,
|
2021-09-07 11:19:57 +00:00
|
|
|
/// The node of the function call.
|
2024-03-06 17:24:13 +11:00
|
|
|
call_hir_id: HirId,
|
2021-09-07 11:19:57 +00:00
|
|
|
/// 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
|
|
|
},
|
|
|
|
|
|
2023-11-23 06:40:40 +00:00
|
|
|
/// Error derived when checking an impl item is compatible with
|
|
|
|
|
/// its corresponding trait item's definition
|
2024-05-09 20:25:11 -04:00
|
|
|
CompareImplItem {
|
2022-04-01 13:38:43 +02:00
|
|
|
impl_item_def_id: LocalDefId,
|
2020-01-22 08:51:01 +01:00
|
|
|
trait_item_def_id: DefId,
|
2022-07-24 19:33:26 +00:00
|
|
|
kind: ty::AssocKind,
|
2020-01-22 08:51:01 +01:00
|
|
|
},
|
|
|
|
|
|
2022-01-08 23:30:19 -05:00
|
|
|
/// Checking that the bounds of a trait's associated type hold for a given impl
|
|
|
|
|
CheckAssociatedTypeBounds {
|
2022-04-01 13:38:43 +02:00
|
|
|
impl_item_def_id: LocalDefId,
|
2022-01-08 23:30:19 -05:00
|
|
|
trait_item_def_id: DefId,
|
|
|
|
|
},
|
|
|
|
|
|
2022-04-01 13:47:01 +02:00
|
|
|
/// Checking that this expression can be assigned to its target.
|
2020-01-22 08:51:01 +01: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,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// Computing common supertype in an if expression
|
2022-07-21 00:03:02 +00:00
|
|
|
IfExpression(Box<IfExpressionCause<'tcx>>),
|
2020-01-22 08:51:01 +01:00
|
|
|
|
|
|
|
|
/// Computing common supertype of an if expression with no else counter-part
|
|
|
|
|
IfExpressionWithNoElse,
|
|
|
|
|
|
|
|
|
|
/// `main` has wrong type
|
|
|
|
|
MainFunctionType,
|
|
|
|
|
|
|
|
|
|
/// `start` has wrong type
|
|
|
|
|
StartFunctionType,
|
|
|
|
|
|
2023-09-17 20:13:05 +02:00
|
|
|
/// language function has wrong type
|
|
|
|
|
LangFunctionType(Symbol),
|
|
|
|
|
|
2020-01-22 08:51:01 +01:00
|
|
|
/// Intrinsic has wrong type
|
|
|
|
|
IntrinsicType,
|
|
|
|
|
|
2021-07-30 11:30:12 -05:00
|
|
|
/// A let else block does not diverge
|
|
|
|
|
LetElse,
|
|
|
|
|
|
2020-01-22 08:51:01 +01:00
|
|
|
/// Method receiver
|
|
|
|
|
MethodReceiver,
|
|
|
|
|
|
2020-06-30 17:41:15 -07:00
|
|
|
UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
|
2020-06-28 15:26:12 -07:00
|
|
|
|
2020-01-22 08:51:01 +01:00
|
|
|
/// `return` with no expression
|
|
|
|
|
ReturnNoExpression,
|
|
|
|
|
|
|
|
|
|
/// `return` with an expression
|
2024-03-06 17:24:13 +11:00
|
|
|
ReturnValue(HirId),
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2022-06-06 23:20:13 -07:00
|
|
|
/// Opaque return type of this function
|
2024-07-31 22:39:40 +00:00
|
|
|
OpaqueReturnType(Option<(Ty<'tcx>, HirId)>),
|
2022-06-06 23:20:13 -07:00
|
|
|
|
2020-01-22 08:51:01 +01:00
|
|
|
/// Block implicit return
|
2024-03-06 17:24:13 +11:00
|
|
|
BlockTailExpression(HirId, hir::MatchSource),
|
2020-01-22 08:51:01 +01:00
|
|
|
|
|
|
|
|
/// #[feature(trivial_bounds)] is not enabled
|
|
|
|
|
TrivialBound,
|
2021-01-08 22:07:49 +00:00
|
|
|
|
2024-03-06 17:24:13 +11:00
|
|
|
AwaitableExpr(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 11:33:49 -05:00
|
|
|
/// Well-formed checking. If a `WellFormedLoc` is provided,
|
2022-06-05 23:04:37 -07: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 11:33:49 -05: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 16:55:39 -04:00
|
|
|
/// This is purely for diagnostic purposes - it is always
|
2024-05-09 20:12:47 -04:00
|
|
|
/// correct to use `Misc` instead, or to specify
|
|
|
|
|
/// `WellFormed(None)`.
|
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 11:33:49 -05:00
|
|
|
WellFormed(Option<WellFormedLoc>),
|
2021-07-15 10:03:39 -04:00
|
|
|
|
|
|
|
|
/// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching against.
|
2021-09-15 22:58:40 -04:00
|
|
|
MatchImpl(ObligationCause<'tcx>, DefId),
|
2022-02-17 19:33:32 +09:00
|
|
|
|
|
|
|
|
BinOp {
|
2024-03-06 17:24:13 +11:00
|
|
|
lhs_hir_id: HirId,
|
|
|
|
|
rhs_hir_id: Option<HirId>,
|
2022-02-17 19:33:32 +09:00
|
|
|
rhs_span: Option<Span>,
|
2023-12-16 17:34:52 -08:00
|
|
|
rhs_is_lit: bool,
|
2022-09-04 22:21:15 +00:00
|
|
|
output_ty: Option<Ty<'tcx>>,
|
2022-02-17 19:33:32 +09:00
|
|
|
},
|
2022-09-16 17:20:11 -04:00
|
|
|
|
|
|
|
|
AscribeUserTypeProvePredicate(Span),
|
2022-07-30 03:41:53 +00:00
|
|
|
|
|
|
|
|
RustCall,
|
2023-04-20 03:56:36 +00:00
|
|
|
|
|
|
|
|
/// Obligations to prove that a `std::ops::Drop` impl is not stronger than
|
|
|
|
|
/// the ADT it's being implemented for.
|
|
|
|
|
DropImpl,
|
2023-05-17 04:05:46 +00:00
|
|
|
|
|
|
|
|
/// Requirement for a `const N: Ty` to implement `Ty: ConstParamTy`
|
|
|
|
|
ConstParam(Ty<'tcx>),
|
2023-03-07 12:03:11 +00:00
|
|
|
|
|
|
|
|
/// Obligations emitted during the normalization of a weak type alias.
|
|
|
|
|
TypeAlias(InternedObligationCauseCode<'tcx>, Span, DefId),
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 15:59:28 +00:00
|
|
|
/// Whether a value can be extracted into a const.
|
|
|
|
|
/// Used for diagnostics around array repeat expressions.
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
|
|
|
|
pub enum IsConstable {
|
|
|
|
|
No,
|
|
|
|
|
/// Call to a const fn
|
|
|
|
|
Fn,
|
|
|
|
|
/// Use of a const ctor
|
|
|
|
|
Ctor,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
crate::TrivialTypeTraversalAndLiftImpls! {
|
|
|
|
|
IsConstable,
|
|
|
|
|
}
|
|
|
|
|
|
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 11:33:49 -05: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>`).
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)]
|
|
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
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 11:33:49 -05:00
|
|
|
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'
|
2024-04-05 22:52:22 -04:00
|
|
|
param_idx: usize,
|
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 11:33:49 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
2024-05-09 20:25:11 -04:00
|
|
|
pub struct ImplDerivedCause<'tcx> {
|
|
|
|
|
pub derived: DerivedCause<'tcx>,
|
2023-02-16 21:55:53 +00:00
|
|
|
/// The `DefId` of the `impl` that gave rise to the `derived` obligation.
|
|
|
|
|
/// If the `derived` obligation arose from a trait alias, which conceptually has a synthetic impl,
|
|
|
|
|
/// then this will be the `DefId` of that trait alias. Care should therefore be taken to handle
|
|
|
|
|
/// that exceptional case where appropriate.
|
|
|
|
|
pub impl_or_alias_def_id: DefId,
|
2023-01-03 19:54:11 -08:00
|
|
|
/// The index of the derived predicate in the parent impl's predicates.
|
|
|
|
|
pub impl_def_predicate_index: Option<usize>,
|
2021-10-13 13:58:41 +00:00
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 11:03:52 +00:00
|
|
|
impl<'tcx> ObligationCauseCode<'tcx> {
|
2022-11-27 11:15:06 +00:00
|
|
|
/// Returns the base obligation, ignoring derived obligations.
|
2020-01-22 08:51:01 +01:00
|
|
|
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 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
base_cause
|
|
|
|
|
}
|
2022-05-10 11:03:52 +00:00
|
|
|
|
2023-12-16 17:34:52 -08:00
|
|
|
/// Returns the base obligation and the base trait predicate, if any, ignoring
|
|
|
|
|
/// derived obligations.
|
|
|
|
|
pub fn peel_derives_with_predicate(&self) -> (&Self, Option<ty::PolyTraitPredicate<'tcx>>) {
|
|
|
|
|
let mut base_cause = self;
|
|
|
|
|
let mut base_trait_pred = None;
|
|
|
|
|
while let Some((parent_code, parent_pred)) = base_cause.parent() {
|
|
|
|
|
base_cause = parent_code;
|
|
|
|
|
if let Some(parent_pred) = parent_pred {
|
|
|
|
|
base_trait_pred = Some(parent_pred);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(base_cause, base_trait_pred)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 11:03:52 +00:00
|
|
|
pub fn parent(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
|
|
|
|
|
match self {
|
2024-05-09 20:12:47 -04:00
|
|
|
ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)),
|
|
|
|
|
ObligationCauseCode::BuiltinDerived(derived)
|
|
|
|
|
| ObligationCauseCode::WellFormedDerived(derived)
|
2024-05-09 20:25:11 -04:00
|
|
|
| ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
|
|
|
|
|
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 {
|
2024-05-09 20:05:59 -04:00
|
|
|
ObligationCauseCode::MatchImpl(cause, _) => cause.code(),
|
2022-08-12 03:13:45 +00:00
|
|
|
_ => self,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
|
2024-04-16 17:02:20 +10:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2024-04-29 14:59:24 +10:00
|
|
|
rustc_data_structures::static_assert_size!(ObligationCauseCode<'_>, 48);
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2020-10-22 10:27:40 -07:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum StatementAsExpression {
|
|
|
|
|
CorrectType,
|
|
|
|
|
NeedsBoxing,
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
2020-01-22 08:51:01 +01:00
|
|
|
pub struct MatchExpressionArmCause<'tcx> {
|
2024-03-06 17:24:13 +11:00
|
|
|
pub arm_block_id: Option<HirId>,
|
2022-07-21 01:26:00 +00:00
|
|
|
pub arm_ty: Ty<'tcx>,
|
2020-01-22 08:51:01 +01:00
|
|
|
pub arm_span: Span,
|
2024-03-06 17:24:13 +11:00
|
|
|
pub prior_arm_block_id: Option<HirId>,
|
2022-07-21 01:26:00 +00:00
|
|
|
pub prior_arm_ty: Ty<'tcx>,
|
|
|
|
|
pub prior_arm_span: Span,
|
2024-10-27 22:53:14 +00:00
|
|
|
/// Span of the scrutinee of the match (the matched value).
|
2020-10-22 14:20:02 -07:00
|
|
|
pub scrut_span: Span,
|
2024-10-27 22:53:14 +00:00
|
|
|
/// Source of the match, i.e. `match` or a desugaring.
|
2020-01-22 08:51:01 +01:00
|
|
|
pub source: hir::MatchSource,
|
2024-10-27 22:53:14 +00:00
|
|
|
/// Span of the *whole* match expr.
|
2024-10-27 22:35:17 +00:00
|
|
|
pub expr_span: Span,
|
2024-10-27 22:53:14 +00:00
|
|
|
/// Spans of the previous arms except for those that diverge (i.e. evaluate to `!`).
|
|
|
|
|
///
|
|
|
|
|
/// These are used for pointing out errors that may affect several arms.
|
2024-02-15 14:34:42 +00:00
|
|
|
pub prior_non_diverging_arms: Vec<Span>,
|
2024-10-27 22:53:14 +00:00
|
|
|
/// Is the expectation of this match expression an RPIT?
|
2024-03-24 12:23:36 -04:00
|
|
|
pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-28 11:38:59 +10:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
|
2022-07-21 00:03:02 +00:00
|
|
|
pub struct IfExpressionCause<'tcx> {
|
2024-03-06 17:24:13 +11:00
|
|
|
pub then_id: HirId,
|
|
|
|
|
pub else_id: HirId,
|
2022-07-21 00:03:02 +00:00
|
|
|
pub then_ty: Ty<'tcx>,
|
|
|
|
|
pub else_ty: Ty<'tcx>,
|
|
|
|
|
pub outer_span: Option<Span>,
|
2024-03-24 12:23:36 -04:00
|
|
|
// Is the expectation of this match expression an RPIT?
|
|
|
|
|
pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
2022-09-11 14:42:43 +02:00
|
|
|
#[derive(TypeVisitable, TypeFoldable)]
|
2024-05-09 20:25:11 -04:00
|
|
|
pub struct DerivedCause<'tcx> {
|
2021-12-24 22:50:44 +08:00
|
|
|
/// The trait predicate of the parent obligation that led to the
|
2020-01-22 08:51:01 +01:00
|
|
|
/// current obligation. Note that only trait obligations lead to
|
2021-12-24 22:50:44 +08:00
|
|
|
/// derived obligations, so we just store the trait predicate here
|
2020-01-22 08:51:01 +01:00
|
|
|
/// directly.
|
2021-12-24 22:50:44 +08:00
|
|
|
pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
2020-01-22 08:51:01 +01:00
|
|
|
|
|
|
|
|
/// The parent trait had this cause.
|
2022-05-10 12:01:56 +00:00
|
|
|
pub parent_code: InternedObligationCauseCode<'tcx>,
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, TypeVisitable)]
|
2020-01-22 08:51:01 +01:00
|
|
|
pub enum SelectionError<'tcx> {
|
2021-10-01 13:05:17 +00:00
|
|
|
/// The trait is not implemented.
|
2020-01-22 08:51:01 +01: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.
|
2024-01-12 16:20:12 +00:00
|
|
|
SignatureMismatch(Box<SignatureMismatchData<'tcx>>),
|
2024-09-25 10:38:40 +02:00
|
|
|
/// The trait pointed by `DefId` is dyn-incompatible.
|
|
|
|
|
TraitDynIncompatible(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 11:55:07 +08:00
|
|
|
Overflow(OverflowError),
|
2023-07-04 11:16:44 +00:00
|
|
|
/// Computing an opaque type's hidden type caused an error (e.g. a cycle error).
|
|
|
|
|
/// We can thus not know whether the hidden type implements an auto trait, so
|
|
|
|
|
/// we should not presume anything about it.
|
|
|
|
|
OpaqueTypeAutoTraitLeakageUnknown(DefId),
|
2024-06-03 03:11:11 +01:00
|
|
|
/// Error for a `ConstArgHasType` goal
|
|
|
|
|
ConstArgHasWrongType { ct: ty::Const<'tcx>, ct_ty: Ty<'tcx>, expected_ty: Ty<'tcx> },
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, Debug, TypeVisitable)]
|
2024-01-12 16:20:12 +00:00
|
|
|
pub struct SignatureMismatchData<'tcx> {
|
2024-04-13 12:34:35 -04:00
|
|
|
pub found_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
|
pub expected_trait_ref: ty::TraitRef<'tcx>,
|
2023-04-30 22:00:16 +02:00
|
|
|
pub terr: ty::error::TypeError<'tcx>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 08:51:01 +01: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 08:51:01 +01: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 08:51:01 +01:00
|
|
|
/// or it may be relative to some bound that is in scope (case B).
|
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2020-01-22 08:51:01 +01:00
|
|
|
/// impl<T:Clone> Clone<T> for Option<T> { ... } // Impl_1
|
|
|
|
|
/// impl<T:Clone> Clone<T> for Box<T> { ... } // Impl_2
|
2020-06-06 12:05:37 +02:00
|
|
|
/// impl Clone for i32 { ... } // Impl_3
|
2020-01-22 08:51:01 +01:00
|
|
|
///
|
2020-06-06 12:05:37 +02:00
|
|
|
/// fn foo<T: Clone>(concrete: Option<Box<i32>>, param: T, mixed: Option<T>) {
|
2021-07-09 18:26:28 +02:00
|
|
|
/// // Case A: ImplSource points at a specific impl. Only possible when
|
2020-06-06 12:05:37 +02:00
|
|
|
/// // type is concretely known. If the impl itself has bounded
|
2021-07-09 18:26:28 +02:00
|
|
|
/// // type parameters, ImplSource will carry resolutions for those as well:
|
2022-03-30 15:14:15 -04:00
|
|
|
/// concrete.clone(); // ImplSource(Impl_1, [ImplSource(Impl_2, [ImplSource(Impl_3)])])
|
2020-01-22 08:51:01 +01:00
|
|
|
///
|
2020-06-06 12:05:37 +02:00
|
|
|
/// // Case B: ImplSource must be provided by caller. This applies when
|
|
|
|
|
/// // type is a type parameter.
|
2020-09-24 19:22:36 +02:00
|
|
|
/// param.clone(); // ImplSource::Param
|
2020-01-22 08:51:01 +01:00
|
|
|
///
|
2020-06-06 12:05:37 +02:00
|
|
|
/// // Case C: A mix of cases A and B.
|
2020-09-24 19:22:36 +02:00
|
|
|
/// mixed.clone(); // ImplSource(Impl_1, [ImplSource::Param])
|
2020-01-22 08:51:01 +01:00
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// ### The type parameter `N`
|
|
|
|
|
///
|
2020-06-02 15:54:24 +00:00
|
|
|
/// See explanation on `ImplSourceUserDefinedData`.
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
|
2022-06-17 10:53:29 +01:00
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-05-11 15:25:33 +00:00
|
|
|
pub enum ImplSource<'tcx, N> {
|
|
|
|
|
/// ImplSource identifying a particular impl.
|
2020-09-24 19:22:36 +02:00
|
|
|
UserDefined(ImplSourceUserDefinedData<'tcx, N>),
|
2020-01-22 08:51:01 +01: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).
|
2024-10-10 19:56:21 +01:00
|
|
|
Param(ThinVec<N>),
|
2020-01-22 08:51:01 +01:00
|
|
|
|
2023-07-24 22:02:52 +00:00
|
|
|
/// Successful resolution for a builtin impl.
|
2024-10-10 19:56:21 +01:00
|
|
|
Builtin(BuiltinImplSource, ThinVec<N>),
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
impl<'tcx, N> ImplSource<'tcx, N> {
|
2024-10-10 19:56:21 +01:00
|
|
|
pub fn nested_obligations(self) -> ThinVec<N> {
|
2020-01-22 08:51:01 +01:00
|
|
|
match self {
|
2020-09-24 19:22:36 +02:00
|
|
|
ImplSource::UserDefined(i) => i.nested,
|
2023-08-13 13:59:19 +00:00
|
|
|
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 16:35:47 -08:00
|
|
|
pub fn borrow_nested_obligations(&self) -> &[N] {
|
2023-05-19 10:32:35 +02:00
|
|
|
match self {
|
|
|
|
|
ImplSource::UserDefined(i) => &i.nested,
|
2023-11-21 20:07:32 +01:00
|
|
|
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
|
2020-02-18 16:35:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 10:32:35 +02:00
|
|
|
pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
|
|
|
|
|
match self {
|
|
|
|
|
ImplSource::UserDefined(i) => &mut i.nested,
|
2023-08-13 13:59:19 +00:00
|
|
|
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
|
2023-05-19 10:32:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
|
2020-01-22 08:51:01 +01:00
|
|
|
where
|
|
|
|
|
F: FnMut(N) -> M,
|
|
|
|
|
{
|
|
|
|
|
match self {
|
2020-09-24 19:22:36 +02:00
|
|
|
ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData {
|
2020-01-22 08:51:01 +01:00
|
|
|
impl_def_id: i.impl_def_id,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: i.args,
|
2020-01-22 08:51:01 +01:00
|
|
|
nested: i.nested.into_iter().map(f).collect(),
|
|
|
|
|
}),
|
2023-08-13 13:59:19 +00:00
|
|
|
ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
|
2023-07-24 22:02:52 +00:00
|
|
|
ImplSource::Builtin(source, n) => {
|
|
|
|
|
ImplSource::Builtin(source, n.into_iter().map(f).collect())
|
2021-08-18 12:45:18 +08:00
|
|
|
}
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Identifies a particular impl in the source, along with a set of
|
2024-02-12 15:39:32 +09:00
|
|
|
/// generic parameters from the impl's type/lifetime parameters. The
|
2020-01-22 08:51:01 +01:00
|
|
|
/// `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.
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
|
2022-06-17 10:53:29 +01:00
|
|
|
#[derive(TypeFoldable, TypeVisitable)]
|
2020-06-02 15:54:24 +00:00
|
|
|
pub struct ImplSourceUserDefinedData<'tcx, N> {
|
2020-01-22 08:51:01 +01:00
|
|
|
pub impl_def_id: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
pub args: GenericArgsRef<'tcx>,
|
2024-10-10 19:56:21 +01:00
|
|
|
pub nested: ThinVec<N>,
|
2020-01-22 08:51:01 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-12 18:49:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
|
2024-09-25 10:38:40 +02:00
|
|
|
pub enum DynCompatibilityViolation {
|
2020-02-10 19:55:49 +01:00
|
|
|
/// `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]>),
|
|
|
|
|
|
2023-03-07 05:51:43 +00:00
|
|
|
// Supertrait has a non-lifetime `for<T>` binder.
|
|
|
|
|
SupertraitNonLifetimeBinder(SmallVec<[Span; 1]>),
|
|
|
|
|
|
2020-02-10 19:55:49 +01:00
|
|
|
/// Method has something illegal.
|
2020-04-19 13:00:18 +02:00
|
|
|
Method(Symbol, MethodViolationCode, Span),
|
2020-02-10 19:55:49 +01:00
|
|
|
|
|
|
|
|
/// Associated const.
|
2020-04-19 13:00:18 +02:00
|
|
|
AssocConst(Symbol, Span),
|
2021-04-27 14:34:23 -04:00
|
|
|
|
|
|
|
|
/// GAT
|
|
|
|
|
GAT(Symbol, Span),
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:38:40 +02:00
|
|
|
impl DynCompatibilityViolation {
|
2020-02-10 19:55:49 +01:00
|
|
|
pub fn error_msg(&self) -> Cow<'static, str> {
|
2022-06-25 14:59:45 -07:00
|
|
|
match self {
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
|
|
|
|
|
DynCompatibilityViolation::SupertraitSelf(ref spans) => {
|
2020-02-10 19:55:49 +01:00
|
|
|
if spans.iter().any(|sp| *sp != DUMMY_SP) {
|
2020-10-19 17:57:18 -07:00
|
|
|
"it uses `Self` as a type parameter".into()
|
2020-02-10 19:55:49 +01:00
|
|
|
} else {
|
|
|
|
|
"it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::SupertraitNonLifetimeBinder(_) => {
|
2023-04-09 23:07:18 +02:00
|
|
|
"where clause cannot reference non-lifetime `for<...>` variables".into()
|
2023-03-07 05:51:43 +00:00
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => {
|
2023-07-25 22:00:13 +02:00
|
|
|
format!("associated function `{name}` has no `self` parameter").into()
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(
|
2020-02-10 19:55:49 +01:00
|
|
|
name,
|
|
|
|
|
MethodViolationCode::ReferencesSelfInput(_),
|
|
|
|
|
DUMMY_SP,
|
2023-07-25 22:00:13 +02:00
|
|
|
) => format!("method `{name}` references the `Self` type in its parameters").into(),
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(
|
|
|
|
|
name,
|
|
|
|
|
MethodViolationCode::ReferencesSelfInput(_),
|
|
|
|
|
_,
|
|
|
|
|
) => format!("method `{name}` references the `Self` type in this parameter").into(),
|
|
|
|
|
DynCompatibilityViolation::Method(
|
|
|
|
|
name,
|
|
|
|
|
MethodViolationCode::ReferencesSelfOutput,
|
|
|
|
|
_,
|
|
|
|
|
) => format!("method `{name}` references the `Self` type in its return type").into(),
|
|
|
|
|
DynCompatibilityViolation::Method(
|
2022-09-11 09:13:55 +00:00
|
|
|
name,
|
2022-11-19 02:32:55 +00:00
|
|
|
MethodViolationCode::ReferencesImplTraitInTrait(_),
|
2022-09-11 09:13:55 +00:00
|
|
|
_,
|
2023-07-25 22:00:13 +02:00
|
|
|
) => {
|
|
|
|
|
format!("method `{name}` references an `impl Trait` type in its return type").into()
|
|
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(name, MethodViolationCode::AsyncFn, _) => {
|
2023-07-25 22:00:13 +02:00
|
|
|
format!("method `{name}` is `async`").into()
|
2022-11-19 02:32:55 +00:00
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(
|
2020-02-10 19:55:49 +01:00
|
|
|
name,
|
|
|
|
|
MethodViolationCode::WhereClauseReferencesSelf,
|
|
|
|
|
_,
|
2023-07-25 22:00:13 +02:00
|
|
|
) => format!("method `{name}` references the `Self` type in its `where` clause").into(),
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(name, MethodViolationCode::Generic, _) => {
|
2023-07-25 22:00:13 +02:00
|
|
|
format!("method `{name}` has generic type parameters").into()
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(
|
2022-06-25 14:59:45 -07:00
|
|
|
name,
|
|
|
|
|
MethodViolationCode::UndispatchableReceiver(_),
|
|
|
|
|
_,
|
2023-07-25 22:00:13 +02:00
|
|
|
) => format!("method `{name}`'s `self` parameter cannot be dispatched on").into(),
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::AssocConst(name, DUMMY_SP) => {
|
2023-07-25 22:00:13 +02:00
|
|
|
format!("it contains associated `const` `{name}`").into()
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::AssocConst(..) => {
|
|
|
|
|
"it contains this associated `const`".into()
|
|
|
|
|
}
|
|
|
|
|
DynCompatibilityViolation::GAT(name, _) => {
|
2023-07-25 22:00:13 +02:00
|
|
|
format!("it contains the generic associated type `{name}`").into()
|
2021-04-27 14:34:23 -04:00
|
|
|
}
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:38:40 +02:00
|
|
|
pub fn solution(&self) -> DynCompatibilityViolationSolution {
|
2022-06-25 14:59:45 -07:00
|
|
|
match self {
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::SizedSelf(_)
|
|
|
|
|
| DynCompatibilityViolation::SupertraitSelf(_)
|
|
|
|
|
| DynCompatibilityViolation::SupertraitNonLifetimeBinder(..) => {
|
|
|
|
|
DynCompatibilityViolationSolution::None
|
2023-10-26 08:20:49 -07:00
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(
|
2020-10-15 17:23:45 -07:00
|
|
|
name,
|
2022-06-25 14:59:45 -07:00
|
|
|
MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
|
2020-10-15 17:23:45 -07:00
|
|
|
_,
|
2024-09-25 10:38:40 +02:00
|
|
|
) => DynCompatibilityViolationSolution::AddSelfOrMakeSized {
|
2023-10-26 08:20:49 -07:00
|
|
|
name: *name,
|
|
|
|
|
add_self_sugg: add_self_sugg.clone(),
|
|
|
|
|
make_sized_sugg: make_sized_sugg.clone(),
|
|
|
|
|
},
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::Method(
|
2020-02-10 19:55:49 +01:00
|
|
|
name,
|
2022-06-25 14:59:45 -07:00
|
|
|
MethodViolationCode::UndispatchableReceiver(Some(span)),
|
|
|
|
|
_,
|
2024-09-25 10:38:40 +02:00
|
|
|
) => DynCompatibilityViolationSolution::ChangeToRefSelf(*name, *span),
|
|
|
|
|
DynCompatibilityViolation::AssocConst(name, _)
|
|
|
|
|
| DynCompatibilityViolation::GAT(name, _)
|
|
|
|
|
| DynCompatibilityViolation::Method(name, ..) => {
|
|
|
|
|
DynCompatibilityViolationSolution::MoveToAnotherTrait(*name)
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
2020-10-15 17:23:45 -07:00
|
|
|
}
|
2020-02-10 19:55:49 +01: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 {
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolation::SupertraitSelf(spans)
|
|
|
|
|
| DynCompatibilityViolation::SizedSelf(spans)
|
|
|
|
|
| DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans) => spans.clone(),
|
|
|
|
|
DynCompatibilityViolation::AssocConst(_, span)
|
|
|
|
|
| DynCompatibilityViolation::GAT(_, span)
|
|
|
|
|
| DynCompatibilityViolation::Method(_, _, span)
|
2020-02-10 19:55:49 +01:00
|
|
|
if *span != DUMMY_SP =>
|
|
|
|
|
{
|
|
|
|
|
smallvec![*span]
|
|
|
|
|
}
|
|
|
|
|
_ => smallvec![],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 08:20:49 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2024-09-25 10:38:40 +02:00
|
|
|
pub enum DynCompatibilityViolationSolution {
|
2023-10-26 08:20:49 -07:00
|
|
|
None,
|
|
|
|
|
AddSelfOrMakeSized {
|
|
|
|
|
name: Symbol,
|
|
|
|
|
add_self_sugg: (String, Span),
|
|
|
|
|
make_sized_sugg: (String, Span),
|
|
|
|
|
},
|
|
|
|
|
ChangeToRefSelf(Symbol, Span),
|
|
|
|
|
MoveToAnotherTrait(Symbol),
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:38:40 +02:00
|
|
|
impl DynCompatibilityViolationSolution {
|
2024-02-23 10:20:45 +11:00
|
|
|
pub fn add_to<G: EmissionGuarantee>(self, err: &mut Diag<'_, G>) {
|
2023-10-26 08:20:49 -07:00
|
|
|
match self {
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolationSolution::None => {}
|
|
|
|
|
DynCompatibilityViolationSolution::AddSelfOrMakeSized {
|
2023-10-26 08:20:49 -07:00
|
|
|
name,
|
|
|
|
|
add_self_sugg,
|
|
|
|
|
make_sized_sugg,
|
|
|
|
|
} => {
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
add_self_sugg.1,
|
|
|
|
|
format!(
|
|
|
|
|
"consider turning `{name}` into a method by giving it a `&self` argument"
|
|
|
|
|
),
|
|
|
|
|
add_self_sugg.0,
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
make_sized_sugg.1,
|
|
|
|
|
format!(
|
|
|
|
|
"alternatively, consider constraining `{name}` so it does not apply to \
|
|
|
|
|
trait objects"
|
|
|
|
|
),
|
|
|
|
|
make_sized_sugg.0,
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolationSolution::ChangeToRefSelf(name, span) => {
|
2023-10-26 08:20:49 -07:00
|
|
|
err.span_suggestion(
|
|
|
|
|
span,
|
|
|
|
|
format!("consider changing method `{name}`'s `self` parameter to be `&self`"),
|
|
|
|
|
"&Self",
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-09-25 10:38:40 +02:00
|
|
|
DynCompatibilityViolationSolution::MoveToAnotherTrait(name) => {
|
2023-10-26 08:20:49 -07:00
|
|
|
err.help(format!("consider moving `{name}` to another trait"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:38:40 +02:00
|
|
|
/// Reasons a method might not be dyn-compatible.
|
2022-06-25 14:59:45 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
|
2020-02-10 19:55:49 +01:00
|
|
|
pub enum MethodViolationCode {
|
|
|
|
|
/// e.g., `fn foo()`
|
2022-06-25 14:59:45 -07:00
|
|
|
StaticMethod(Option<(/* add &self */ (String, Span), /* add Self: Sized */ (String, Span))>),
|
2020-02-10 19:55:49 +01:00
|
|
|
|
|
|
|
|
/// e.g., `fn foo(&self, x: Self)`
|
2022-06-25 14:59:45 -07:00
|
|
|
ReferencesSelfInput(Option<Span>),
|
2020-02-10 19:55:49 +01:00
|
|
|
|
|
|
|
|
/// e.g., `fn foo(&self) -> Self`
|
|
|
|
|
ReferencesSelfOutput,
|
|
|
|
|
|
2022-09-11 09:13:55 +00:00
|
|
|
/// e.g., `fn foo(&self) -> impl Sized`
|
2022-11-19 02:32:55 +00:00
|
|
|
ReferencesImplTraitInTrait(Span),
|
|
|
|
|
|
|
|
|
|
/// e.g., `async fn foo(&self)`
|
|
|
|
|
AsyncFn,
|
2022-09-11 09:13:55 +00:00
|
|
|
|
2020-02-10 19:55:49 +01: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 14:59:45 -07:00
|
|
|
UndispatchableReceiver(Option<Span>),
|
2020-02-10 19:55:49 +01:00
|
|
|
}
|
2022-05-01 11:03:14 +02:00
|
|
|
|
2022-09-09 13:36:27 +02:00
|
|
|
/// These are the error cases for `codegen_select_candidate`.
|
2022-05-01 11:03:14 +02: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,
|
|
|
|
|
}
|