rustc_middle/traits/
mod.rs

1//! Trait Resolution. See the [rustc dev guide] for more information on how this works.
2//!
3//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
4
5pub mod query;
6pub mod select;
7pub mod solve;
8pub mod specialization_graph;
9mod structural_impls;
10
11use std::borrow::Cow;
12use std::hash::{Hash, Hasher};
13use std::sync::Arc;
14
15use rustc_errors::{Applicability, Diag, EmissionGuarantee, ErrorGuaranteed};
16use rustc_hir as hir;
17use rustc_hir::HirId;
18use rustc_hir::def_id::DefId;
19use rustc_macros::{
20    Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
21};
22use rustc_span::def_id::{CRATE_DEF_ID, LocalDefId};
23use rustc_span::{DUMMY_SP, Span, Symbol};
24use smallvec::{SmallVec, smallvec};
25use thin_vec::ThinVec;
26
27pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache};
28use crate::mir::ConstraintCategory;
29pub use crate::traits::solve::BuiltinImplSource;
30use crate::ty::abstract_const::NotConstEvaluatable;
31use crate::ty::{self, AdtKind, GenericArgsRef, Ty};
32
33/// The reason why we incurred this obligation; used for error reporting.
34///
35/// Non-misc `ObligationCauseCode`s are stored on the heap. This gives the
36/// best trade-off between keeping the type small (which makes copies cheaper)
37/// while not doing too many heap allocations.
38///
39/// We do not want to intern this as there are a lot of obligation causes which
40/// only live for a short period of time.
41#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
42#[derive(TypeVisitable, TypeFoldable)]
43pub struct ObligationCause<'tcx> {
44    pub span: Span,
45
46    /// The ID of the fn body that triggered this obligation. This is
47    /// used for region obligations to determine the precise
48    /// environment in which the region obligation should be evaluated
49    /// (in particular, closures can add new assumptions). See the
50    /// field `region_obligations` of the `FulfillmentContext` for more
51    /// information.
52    pub body_id: LocalDefId,
53
54    code: ObligationCauseCodeHandle<'tcx>,
55}
56
57// This custom hash function speeds up hashing for `Obligation` deduplication
58// greatly by skipping the `code` field, which can be large and complex. That
59// shouldn't affect hash quality much since there are several other fields in
60// `Obligation` which should be unique enough, especially the predicate itself
61// which is hashed as an interned pointer. See #90996.
62impl Hash for ObligationCause<'_> {
63    fn hash<H: Hasher>(&self, state: &mut H) {
64        self.body_id.hash(state);
65        self.span.hash(state);
66    }
67}
68
69impl<'tcx> ObligationCause<'tcx> {
70    #[inline]
71    pub fn new(
72        span: Span,
73        body_id: LocalDefId,
74        code: ObligationCauseCode<'tcx>,
75    ) -> ObligationCause<'tcx> {
76        ObligationCause { span, body_id, code: code.into() }
77    }
78
79    pub fn misc(span: Span, body_id: LocalDefId) -> ObligationCause<'tcx> {
80        ObligationCause::new(span, body_id, ObligationCauseCode::Misc)
81    }
82
83    #[inline(always)]
84    pub fn dummy() -> ObligationCause<'tcx> {
85        ObligationCause::dummy_with_span(DUMMY_SP)
86    }
87
88    #[inline(always)]
89    pub fn dummy_with_span(span: Span) -> ObligationCause<'tcx> {
90        ObligationCause { span, body_id: CRATE_DEF_ID, code: Default::default() }
91    }
92
93    #[inline]
94    pub fn code(&self) -> &ObligationCauseCode<'tcx> {
95        &self.code
96    }
97
98    pub fn map_code(
99        &mut self,
100        f: impl FnOnce(ObligationCauseCodeHandle<'tcx>) -> ObligationCauseCode<'tcx>,
101    ) {
102        self.code = f(std::mem::take(&mut self.code)).into();
103    }
104
105    pub fn derived_cause(
106        mut self,
107        parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
108        variant: impl FnOnce(DerivedCause<'tcx>) -> ObligationCauseCode<'tcx>,
109    ) -> ObligationCause<'tcx> {
110        /*!
111         * Creates a cause for obligations that are derived from
112         * `obligation` by a recursive search (e.g., for a builtin
113         * bound, or eventually a `auto trait Foo`). If `obligation`
114         * is itself a derived obligation, this is just a clone, but
115         * otherwise we create a "derived obligation" cause so as to
116         * keep track of the original root obligation for error
117         * reporting.
118         */
119
120        // NOTE(flaper87): As of now, it keeps track of the whole error
121        // chain. Ideally, we should have a way to configure this either
122        // by using -Z verbose-internals or just a CLI argument.
123        self.code = variant(DerivedCause { parent_trait_pred, parent_code: self.code }).into();
124        self
125    }
126
127    pub fn derived_host_cause(
128        mut self,
129        parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
130        variant: impl FnOnce(DerivedHostCause<'tcx>) -> ObligationCauseCode<'tcx>,
131    ) -> ObligationCause<'tcx> {
132        self.code = variant(DerivedHostCause { parent_host_pred, parent_code: self.code }).into();
133        self
134    }
135
136    pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
137        match self.code() {
138            ObligationCauseCode::MatchImpl(cause, _) => cause.to_constraint_category(),
139            ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span) => {
140                ConstraintCategory::Predicate(*predicate_span)
141            }
142            _ => ConstraintCategory::BoringNoLocation,
143        }
144    }
145}
146
147/// A compact form of `ObligationCauseCode`.
148#[derive(Clone, PartialEq, Eq, Default, HashStable)]
149#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
150pub struct ObligationCauseCodeHandle<'tcx> {
151    /// `None` for `ObligationCauseCode::Misc` (a common case, occurs ~60% of
152    /// the time). `Some` otherwise.
153    code: Option<Arc<ObligationCauseCode<'tcx>>>,
154}
155
156impl<'tcx> std::fmt::Debug for ObligationCauseCodeHandle<'tcx> {
157    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158        let cause: &ObligationCauseCode<'_> = self;
159        cause.fmt(f)
160    }
161}
162
163impl<'tcx> ObligationCauseCode<'tcx> {
164    #[inline(always)]
165    fn into(self) -> ObligationCauseCodeHandle<'tcx> {
166        ObligationCauseCodeHandle {
167            code: if let ObligationCauseCode::Misc = self { None } else { Some(Arc::new(self)) },
168        }
169    }
170}
171
172impl<'tcx> std::ops::Deref for ObligationCauseCodeHandle<'tcx> {
173    type Target = ObligationCauseCode<'tcx>;
174
175    fn deref(&self) -> &Self::Target {
176        self.code.as_deref().unwrap_or(&ObligationCauseCode::Misc)
177    }
178}
179
180#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
181#[derive(TypeVisitable, TypeFoldable)]
182pub enum ObligationCauseCode<'tcx> {
183    /// Not well classified or should be obvious from the span.
184    Misc,
185
186    /// A slice or array is WF only if `T: Sized`.
187    SliceOrArrayElem,
188
189    /// An array `[T; N]` can only be indexed (and is only well-formed if) `N` has type usize.
190    ArrayLen(Ty<'tcx>),
191
192    /// A tuple is WF only if its middle elements are `Sized`.
193    TupleElem,
194
195    /// Represents a clause that comes from a specific item.
196    /// The span corresponds to the clause.
197    WhereClause(DefId, Span),
198
199    /// Represents a bound for an opaque we are checking the well-formedness of.
200    /// The def-id corresponds to a specific definition site that we found the
201    /// hidden type from, if any.
202    OpaqueTypeBound(Span, Option<LocalDefId>),
203
204    /// Like `WhereClause`, but also identifies the expression
205    /// which requires the `where` clause to be proven, and also
206    /// identifies the index of the predicate in the `predicates_of`
207    /// list of the item.
208    WhereClauseInExpr(DefId, Span, HirId, usize),
209
210    /// Like `WhereClauseinExpr`, but indexes into the `const_conditions`
211    /// rather than the `predicates_of`.
212    HostEffectInExpr(DefId, Span, HirId, usize),
213
214    /// A type like `&'a T` is WF only if `T: 'a`.
215    ReferenceOutlivesReferent(Ty<'tcx>),
216
217    /// A type like `Box<Foo<'a> + 'b>` is WF only if `'b: 'a`.
218    ObjectTypeBound(Ty<'tcx>, ty::Region<'tcx>),
219
220    /// Obligation incurred due to a coercion.
221    Coercion {
222        source: Ty<'tcx>,
223        target: Ty<'tcx>,
224    },
225
226    /// Various cases where expressions must be `Sized` / `Copy` / etc.
227    /// `L = X` implies that `L` is `Sized`.
228    AssignmentLhsSized,
229    /// `(x1, .., xn)` must be `Sized`.
230    TupleInitializerSized,
231    /// `S { ... }` must be `Sized`.
232    StructInitializerSized,
233    /// Type of each variable must be `Sized`.
234    VariableType(HirId),
235    /// Argument type must be `Sized`.
236    SizedArgumentType(Option<HirId>),
237    /// Return type must be `Sized`.
238    SizedReturnType,
239    /// Return type of a call expression must be `Sized`.
240    SizedCallReturnType,
241    /// Yield type must be `Sized`.
242    SizedYieldType,
243    /// Inline asm operand type must be `Sized`.
244    InlineAsmSized,
245    /// Captured closure type must be `Sized`.
246    SizedClosureCapture(LocalDefId),
247    /// Types live across coroutine yields must be `Sized`.
248    SizedCoroutineInterior(LocalDefId),
249    /// `[expr; N]` requires `type_of(expr): Copy`.
250    RepeatElementCopy {
251        /// If element is a `const fn` or const ctor we display a help message suggesting
252        /// to move it to a new `const` item while saying that `T` doesn't implement `Copy`.
253        is_constable: IsConstable,
254
255        /// Span of the repeat element.
256        ///
257        /// This is used to suggest wrapping it in a `const { ... }` block.
258        elt_span: Span,
259    },
260
261    /// Types of fields (other than the last, except for packed structs) in a struct must be sized.
262    FieldSized {
263        adt_kind: AdtKind,
264        span: Span,
265        last: bool,
266    },
267
268    /// Constant expressions must be sized.
269    SizedConstOrStatic,
270
271    /// `static` items must have `Sync` type.
272    SharedStatic,
273
274    /// Derived obligation (i.e. theoretical `where` clause) on a built-in
275    /// implementation like `Copy` or `Sized`.
276    BuiltinDerived(DerivedCause<'tcx>),
277
278    /// Derived obligation (i.e. `where` clause) on an user-provided impl
279    /// or a trait alias.
280    ImplDerived(Box<ImplDerivedCause<'tcx>>),
281
282    /// Derived obligation for WF goals.
283    WellFormedDerived(DerivedCause<'tcx>),
284
285    /// Derived obligation (i.e. `where` clause) on an user-provided impl
286    /// or a trait alias.
287    ImplDerivedHost(Box<ImplDerivedHostCause<'tcx>>),
288
289    /// Derived obligation (i.e. `where` clause) on an user-provided impl
290    /// or a trait alias.
291    BuiltinDerivedHost(DerivedHostCause<'tcx>),
292
293    /// Derived obligation refined to point at a specific argument in
294    /// a call or method expression.
295    FunctionArg {
296        /// The node of the relevant argument in the function call.
297        arg_hir_id: HirId,
298        /// The node of the function call.
299        call_hir_id: HirId,
300        /// The obligation introduced by this argument.
301        parent_code: ObligationCauseCodeHandle<'tcx>,
302    },
303
304    /// Error derived when checking an impl item is compatible with
305    /// its corresponding trait item's definition
306    CompareImplItem {
307        impl_item_def_id: LocalDefId,
308        trait_item_def_id: DefId,
309        kind: ty::AssocKind,
310    },
311
312    /// Checking that the bounds of a trait's associated type hold for a given impl
313    CheckAssociatedTypeBounds {
314        impl_item_def_id: LocalDefId,
315        trait_item_def_id: DefId,
316    },
317
318    /// Checking that this expression can be assigned to its target.
319    ExprAssignable,
320
321    /// Computing common supertype in the arms of a match expression
322    MatchExpressionArm(Box<MatchExpressionArmCause<'tcx>>),
323
324    /// Type error arising from type checking a pattern against an expected type.
325    Pattern {
326        /// The span of the scrutinee or type expression which caused the `root_ty` type.
327        span: Option<Span>,
328        /// The root expected type induced by a scrutinee or type expression.
329        root_ty: Ty<'tcx>,
330        /// Information about the `Span`, if it came from an expression, otherwise `None`.
331        origin_expr: Option<PatternOriginExpr>,
332    },
333
334    /// Computing common supertype in an if expression
335    IfExpression {
336        expr_id: HirId,
337        // Is the expectation of this match expression an RPIT?
338        tail_defines_return_position_impl_trait: Option<LocalDefId>,
339    },
340
341    /// Computing common supertype of an if expression with no else counter-part
342    IfExpressionWithNoElse,
343
344    /// `main` has wrong type
345    MainFunctionType,
346
347    /// language function has wrong type
348    LangFunctionType(Symbol),
349
350    /// Intrinsic has wrong type
351    IntrinsicType,
352
353    /// A let else block does not diverge
354    LetElse,
355
356    /// Method receiver
357    MethodReceiver,
358
359    /// `return` with no expression
360    ReturnNoExpression,
361
362    /// `return` with an expression
363    ReturnValue(HirId),
364
365    /// Opaque return type of this function
366    OpaqueReturnType(Option<(Ty<'tcx>, HirId)>),
367
368    /// Block implicit return
369    BlockTailExpression(HirId, hir::MatchSource),
370
371    /// #[feature(trivial_bounds)] is not enabled
372    TrivialBound,
373
374    AwaitableExpr(HirId),
375
376    ForLoopIterator,
377
378    QuestionMark,
379
380    /// Well-formed checking. If a `WellFormedLoc` is provided,
381    /// then it will be used to perform HIR-based wf checking
382    /// after an error occurs, in order to generate a more precise error span.
383    /// This is purely for diagnostic purposes - it is always
384    /// correct to use `Misc` instead, or to specify
385    /// `WellFormed(None)`.
386    WellFormed(Option<WellFormedLoc>),
387
388    /// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching
389    /// against.
390    MatchImpl(ObligationCause<'tcx>, DefId),
391
392    UnOp {
393        hir_id: HirId,
394    },
395
396    BinOp {
397        lhs_hir_id: HirId,
398        rhs_hir_id: HirId,
399        rhs_span: Span,
400        rhs_is_lit: bool,
401        output_ty: Option<Ty<'tcx>>,
402    },
403
404    AscribeUserTypeProvePredicate(Span),
405
406    RustCall,
407
408    DynCompatible(Span),
409
410    /// Obligations to prove that a `Drop` or negative auto trait impl is not stronger than
411    /// the ADT it's being implemented for.
412    AlwaysApplicableImpl,
413
414    /// Requirement for a `const N: Ty` to implement `Ty: ConstParamTy`
415    ConstParam(Ty<'tcx>),
416
417    /// Obligations emitted during the normalization of a free type alias.
418    TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
419
420    /// Only reachable if the `unsized_fn_params` feature is used. Unsized function arguments must
421    /// be place expressions because we can't store them in MIR locals as temporaries.
422    UnsizedNonPlaceExpr(Span),
423
424    /// Error derived when checking an impl item is compatible with
425    /// its corresponding trait item's definition
426    CompareEii {
427        external_impl: LocalDefId,
428        declaration: DefId,
429    },
430}
431
432/// Whether a value can be extracted into a const.
433/// Used for diagnostics around array repeat expressions.
434#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
435pub enum IsConstable {
436    No,
437    /// Call to a const fn
438    Fn,
439    /// Use of a const ctor
440    Ctor,
441}
442
443/// The 'location' at which we try to perform HIR-based wf checking.
444/// This information is used to obtain an `hir::Ty`, which
445/// we can walk in order to obtain precise spans for any
446/// 'nested' types (e.g. `Foo` in `Option<Foo>`).
447#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)]
448#[derive(TypeVisitable, TypeFoldable)]
449pub enum WellFormedLoc {
450    /// Use the type of the provided definition.
451    Ty(LocalDefId),
452    /// Use the type of the parameter of the provided function.
453    /// We cannot use `hir::Param`, since the function may
454    /// not have a body (e.g. a trait method definition)
455    Param {
456        /// The function to lookup the parameter in
457        function: LocalDefId,
458        /// The index of the parameter to use.
459        /// Parameters are indexed from 0, with the return type
460        /// being the last 'parameter'
461        param_idx: usize,
462    },
463}
464
465impl<'tcx> ObligationCauseCode<'tcx> {
466    /// Returns the base obligation, ignoring derived obligations.
467    pub fn peel_derives(&self) -> &Self {
468        let mut base_cause = self;
469        while let Some(parent_code) = base_cause.parent() {
470            base_cause = parent_code;
471        }
472        base_cause
473    }
474
475    pub fn parent(&self) -> Option<&Self> {
476        match self {
477            ObligationCauseCode::FunctionArg { parent_code, .. } => Some(parent_code),
478            ObligationCauseCode::BuiltinDerived(derived)
479            | ObligationCauseCode::WellFormedDerived(derived)
480            | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
481                Some(&derived.parent_code)
482            }
483            ObligationCauseCode::BuiltinDerivedHost(derived)
484            | ObligationCauseCode::ImplDerivedHost(box ImplDerivedHostCause { derived, .. }) => {
485                Some(&derived.parent_code)
486            }
487            _ => None,
488        }
489    }
490
491    /// Returns the base obligation and the base trait predicate, if any, ignoring
492    /// derived obligations.
493    pub fn peel_derives_with_predicate(&self) -> (&Self, Option<ty::PolyTraitPredicate<'tcx>>) {
494        let mut base_cause = self;
495        let mut base_trait_pred = None;
496        while let Some((parent_code, parent_pred)) = base_cause.parent_with_predicate() {
497            base_cause = parent_code;
498            if let Some(parent_pred) = parent_pred {
499                base_trait_pred = Some(parent_pred);
500            }
501        }
502
503        (base_cause, base_trait_pred)
504    }
505
506    pub fn parent_with_predicate(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
507        match self {
508            ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)),
509            ObligationCauseCode::BuiltinDerived(derived)
510            | ObligationCauseCode::WellFormedDerived(derived)
511            | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
512                Some((&derived.parent_code, Some(derived.parent_trait_pred)))
513            }
514            _ => None,
515        }
516    }
517
518    pub fn peel_match_impls(&self) -> &Self {
519        match self {
520            ObligationCauseCode::MatchImpl(cause, _) => cause.code(),
521            _ => self,
522        }
523    }
524}
525
526// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
527#[cfg(target_pointer_width = "64")]
528rustc_data_structures::static_assert_size!(ObligationCauseCode<'_>, 48);
529
530#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
531#[derive(TypeVisitable, TypeFoldable)]
532pub struct MatchExpressionArmCause<'tcx> {
533    pub arm_block_id: Option<HirId>,
534    pub arm_ty: Ty<'tcx>,
535    pub arm_span: Span,
536    pub prior_arm_block_id: Option<HirId>,
537    pub prior_arm_ty: Ty<'tcx>,
538    pub prior_arm_span: Span,
539    /// Span of the scrutinee of the match (the matched value).
540    pub scrut_span: Span,
541    /// Source of the match, i.e. `match` or a desugaring.
542    pub source: hir::MatchSource,
543    /// Span of the *whole* match expr.
544    pub expr_span: Span,
545    /// Spans of the previous arms except for those that diverge (i.e. evaluate to `!`).
546    ///
547    /// These are used for pointing out errors that may affect several arms.
548    pub prior_non_diverging_arms: Vec<Span>,
549    /// Is the expectation of this match expression an RPIT?
550    pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
551}
552
553/// Information about the origin expression of a pattern, relevant to diagnostics.
554/// Fields here refer to the scrutinee of a pattern.
555/// If the scrutinee isn't given in the diagnostic, then this won't exist.
556#[derive(Copy, Clone, Debug, PartialEq, Eq)]
557#[derive(TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
558pub struct PatternOriginExpr {
559    /// A span representing the scrutinee expression, with all leading references
560    /// peeled from the expression.
561    /// Only references in the expression are peeled - if the expression refers to a variable
562    /// whose type is a reference, then that reference is kept because it wasn't created
563    /// in the expression.
564    pub peeled_span: Span,
565    /// The number of references that were peeled to produce `peeled_span`.
566    pub peeled_count: usize,
567    /// Does the peeled expression need to be wrapped in parentheses for
568    /// a prefix suggestion (i.e., dereference) to be valid.
569    pub peeled_prefix_suggestion_parentheses: bool,
570}
571
572#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
573#[derive(TypeVisitable, TypeFoldable)]
574pub struct DerivedCause<'tcx> {
575    /// The trait predicate of the parent obligation that led to the
576    /// current obligation. Note that only trait obligations lead to
577    /// derived obligations, so we just store the trait predicate here
578    /// directly.
579    pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
580
581    /// The parent trait had this cause.
582    pub parent_code: ObligationCauseCodeHandle<'tcx>,
583}
584
585#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
586#[derive(TypeVisitable, TypeFoldable)]
587pub struct ImplDerivedCause<'tcx> {
588    pub derived: DerivedCause<'tcx>,
589    /// The `DefId` of the `impl` that gave rise to the `derived` obligation.
590    /// If the `derived` obligation arose from a trait alias, which conceptually has a synthetic
591    /// impl, then this will be the `DefId` of that trait alias. Care should therefore be taken to
592    /// handle that exceptional case where appropriate.
593    pub impl_or_alias_def_id: DefId,
594    /// The index of the derived predicate in the parent impl's predicates.
595    pub impl_def_predicate_index: Option<usize>,
596    pub span: Span,
597}
598
599#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
600#[derive(TypeVisitable, TypeFoldable)]
601pub struct DerivedHostCause<'tcx> {
602    /// The trait predicate of the parent obligation that led to the
603    /// current obligation. Note that only trait obligations lead to
604    /// derived obligations, so we just store the trait predicate here
605    /// directly.
606    pub parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
607
608    /// The parent trait had this cause.
609    pub parent_code: ObligationCauseCodeHandle<'tcx>,
610}
611
612#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
613#[derive(TypeVisitable, TypeFoldable)]
614pub struct ImplDerivedHostCause<'tcx> {
615    pub derived: DerivedHostCause<'tcx>,
616    /// The `DefId` of the `impl` that gave rise to the `derived` obligation.
617    pub impl_def_id: DefId,
618    pub span: Span,
619}
620
621#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
622pub enum SelectionError<'tcx> {
623    /// The trait is not implemented.
624    Unimplemented,
625    /// After a closure impl has selected, its "outputs" were evaluated
626    /// (which for closures includes the "input" type params) and they
627    /// didn't resolve. See `confirm_poly_trait_refs` for more.
628    SignatureMismatch(Box<SignatureMismatchData<'tcx>>),
629    /// The trait pointed by `DefId` is dyn-incompatible.
630    TraitDynIncompatible(DefId),
631    /// A given constant couldn't be evaluated.
632    NotConstEvaluatable(NotConstEvaluatable),
633    /// Exceeded the recursion depth during type projection.
634    Overflow(OverflowError),
635    /// Computing an opaque type's hidden type caused an error (e.g. a cycle error).
636    /// We can thus not know whether the hidden type implements an auto trait, so
637    /// we should not presume anything about it.
638    OpaqueTypeAutoTraitLeakageUnknown(DefId),
639    /// Error for a `ConstArgHasType` goal
640    ConstArgHasWrongType { ct: ty::Const<'tcx>, ct_ty: Ty<'tcx>, expected_ty: Ty<'tcx> },
641}
642
643#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
644pub struct SignatureMismatchData<'tcx> {
645    pub found_trait_ref: ty::TraitRef<'tcx>,
646    pub expected_trait_ref: ty::TraitRef<'tcx>,
647    pub terr: ty::error::TypeError<'tcx>,
648}
649
650/// When performing resolution, it is typically the case that there
651/// can be one of three outcomes:
652///
653/// - `Ok(Some(r))`: success occurred with result `r`
654/// - `Ok(None)`: could not definitely determine anything, usually due
655///   to inconclusive type inference.
656/// - `Err(e)`: error `e` occurred
657pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
658
659/// Given the successful resolution of an obligation, the `ImplSource`
660/// indicates where the impl comes from.
661///
662/// For example, the obligation may be satisfied by a specific impl (case A),
663/// or it may be relative to some bound that is in scope (case B).
664///
665/// ```ignore (illustrative)
666/// impl<T:Clone> Clone<T> for Option<T> { ... } // Impl_1
667/// impl<T:Clone> Clone<T> for Box<T> { ... }    // Impl_2
668/// impl Clone for i32 { ... }                   // Impl_3
669///
670/// fn foo<T: Clone>(concrete: Option<Box<i32>>, param: T, mixed: Option<T>) {
671///     // Case A: ImplSource points at a specific impl. Only possible when
672///     // type is concretely known. If the impl itself has bounded
673///     // type parameters, ImplSource will carry resolutions for those as well:
674///     concrete.clone(); // ImplSource(Impl_1, [ImplSource(Impl_2, [ImplSource(Impl_3)])])
675///
676///     // Case B: ImplSource must be provided by caller. This applies when
677///     // type is a type parameter.
678///     param.clone();    // ImplSource::Param
679///
680///     // Case C: A mix of cases A and B.
681///     mixed.clone();    // ImplSource(Impl_1, [ImplSource::Param])
682/// }
683/// ```
684///
685/// ### The type parameter `N`
686///
687/// See explanation on `ImplSourceUserDefinedData`.
688#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
689#[derive(TypeFoldable, TypeVisitable)]
690pub enum ImplSource<'tcx, N> {
691    /// ImplSource identifying a particular impl.
692    UserDefined(ImplSourceUserDefinedData<'tcx, N>),
693
694    /// Successful resolution to an obligation provided by the caller
695    /// for some type parameter. The `Vec<N>` represents the
696    /// obligations incurred from normalizing the where-clause (if
697    /// any).
698    Param(ThinVec<N>),
699
700    /// Successful resolution for a builtin impl.
701    Builtin(BuiltinImplSource, ThinVec<N>),
702}
703
704impl<'tcx, N> ImplSource<'tcx, N> {
705    pub fn nested_obligations(self) -> ThinVec<N> {
706        match self {
707            ImplSource::UserDefined(i) => i.nested,
708            ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
709        }
710    }
711
712    pub fn borrow_nested_obligations(&self) -> &[N] {
713        match self {
714            ImplSource::UserDefined(i) => &i.nested,
715            ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
716        }
717    }
718
719    pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
720        match self {
721            ImplSource::UserDefined(i) => &mut i.nested,
722            ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
723        }
724    }
725
726    pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
727    where
728        F: FnMut(N) -> M,
729    {
730        match self {
731            ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData {
732                impl_def_id: i.impl_def_id,
733                args: i.args,
734                nested: i.nested.into_iter().map(f).collect(),
735            }),
736            ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
737            ImplSource::Builtin(source, n) => {
738                ImplSource::Builtin(source, n.into_iter().map(f).collect())
739            }
740        }
741    }
742}
743
744/// Identifies a particular impl in the source, along with a set of
745/// generic parameters from the impl's type/lifetime parameters. The
746/// `nested` vector corresponds to the nested obligations attached to
747/// the impl's type parameters.
748///
749/// The type parameter `N` indicates the type used for "nested
750/// obligations" that are required by the impl. During type-check, this
751/// is `Obligation`, as one might expect. During codegen, however, this
752/// is `()`, because codegen only requires a shallow resolution of an
753/// impl, and nested obligations are satisfied later.
754#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
755#[derive(TypeFoldable, TypeVisitable)]
756pub struct ImplSourceUserDefinedData<'tcx, N> {
757    pub impl_def_id: DefId,
758    pub args: GenericArgsRef<'tcx>,
759    pub nested: ThinVec<N>,
760}
761
762#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
763pub enum DynCompatibilityViolation {
764    /// `Self: Sized` declared on the trait.
765    SizedSelf(SmallVec<[Span; 1]>),
766
767    /// Supertrait reference references `Self` an in illegal location
768    /// (e.g., `trait Foo : Bar<Self>`).
769    SupertraitSelf(SmallVec<[Span; 1]>),
770
771    // Supertrait has a non-lifetime `for<T>` binder.
772    SupertraitNonLifetimeBinder(SmallVec<[Span; 1]>),
773
774    // Trait has a `const Trait` supertrait.
775    SupertraitConst(SmallVec<[Span; 1]>),
776
777    /// Method has something illegal.
778    Method(Symbol, MethodViolationCode, Span),
779
780    /// Associated const.
781    AssocConst(Symbol, Span),
782
783    /// GAT
784    GAT(Symbol, Span),
785}
786
787impl DynCompatibilityViolation {
788    pub fn error_msg(&self) -> Cow<'static, str> {
789        match self {
790            DynCompatibilityViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
791            DynCompatibilityViolation::SupertraitSelf(spans) => {
792                if spans.iter().any(|sp| *sp != DUMMY_SP) {
793                    "it uses `Self` as a type parameter".into()
794                } else {
795                    "it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
796                        .into()
797                }
798            }
799            DynCompatibilityViolation::SupertraitNonLifetimeBinder(_) => {
800                "where clause cannot reference non-lifetime `for<...>` variables".into()
801            }
802            DynCompatibilityViolation::SupertraitConst(_) => {
803                "it cannot have a `const` supertrait".into()
804            }
805            DynCompatibilityViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => {
806                format!("associated function `{name}` has no `self` parameter").into()
807            }
808            DynCompatibilityViolation::Method(
809                name,
810                MethodViolationCode::ReferencesSelfInput(_),
811                DUMMY_SP,
812            ) => format!("method `{name}` references the `Self` type in its parameters").into(),
813            DynCompatibilityViolation::Method(
814                name,
815                MethodViolationCode::ReferencesSelfInput(_),
816                _,
817            ) => format!("method `{name}` references the `Self` type in this parameter").into(),
818            DynCompatibilityViolation::Method(
819                name,
820                MethodViolationCode::ReferencesSelfOutput,
821                _,
822            ) => format!("method `{name}` references the `Self` type in its return type").into(),
823            DynCompatibilityViolation::Method(
824                name,
825                MethodViolationCode::ReferencesImplTraitInTrait(_),
826                _,
827            ) => {
828                format!("method `{name}` references an `impl Trait` type in its return type").into()
829            }
830            DynCompatibilityViolation::Method(name, MethodViolationCode::AsyncFn, _) => {
831                format!("method `{name}` is `async`").into()
832            }
833            DynCompatibilityViolation::Method(name, MethodViolationCode::CVariadic, _) => {
834                format!("method `{name}` is C-variadic").into()
835            }
836            DynCompatibilityViolation::Method(
837                name,
838                MethodViolationCode::WhereClauseReferencesSelf,
839                _,
840            ) => format!("method `{name}` references the `Self` type in its `where` clause").into(),
841            DynCompatibilityViolation::Method(name, MethodViolationCode::Generic, _) => {
842                format!("method `{name}` has generic type parameters").into()
843            }
844            DynCompatibilityViolation::Method(
845                name,
846                MethodViolationCode::UndispatchableReceiver(_),
847                _,
848            ) => format!("method `{name}`'s `self` parameter cannot be dispatched on").into(),
849            DynCompatibilityViolation::AssocConst(name, DUMMY_SP) => {
850                format!("it contains associated `const` `{name}`").into()
851            }
852            DynCompatibilityViolation::AssocConst(..) => {
853                "it contains this associated `const`".into()
854            }
855            DynCompatibilityViolation::GAT(name, _) => {
856                format!("it contains the generic associated type `{name}`").into()
857            }
858        }
859    }
860
861    pub fn solution(&self) -> DynCompatibilityViolationSolution {
862        match self {
863            DynCompatibilityViolation::SizedSelf(_)
864            | DynCompatibilityViolation::SupertraitSelf(_)
865            | DynCompatibilityViolation::SupertraitNonLifetimeBinder(..)
866            | DynCompatibilityViolation::SupertraitConst(_) => {
867                DynCompatibilityViolationSolution::None
868            }
869            DynCompatibilityViolation::Method(
870                name,
871                MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
872                _,
873            ) => DynCompatibilityViolationSolution::AddSelfOrMakeSized {
874                name: *name,
875                add_self_sugg: add_self_sugg.clone(),
876                make_sized_sugg: make_sized_sugg.clone(),
877            },
878            DynCompatibilityViolation::Method(
879                name,
880                MethodViolationCode::UndispatchableReceiver(Some(span)),
881                _,
882            ) => DynCompatibilityViolationSolution::ChangeToRefSelf(*name, *span),
883            DynCompatibilityViolation::AssocConst(name, _)
884            | DynCompatibilityViolation::GAT(name, _)
885            | DynCompatibilityViolation::Method(name, ..) => {
886                DynCompatibilityViolationSolution::MoveToAnotherTrait(*name)
887            }
888        }
889    }
890
891    pub fn spans(&self) -> SmallVec<[Span; 1]> {
892        // When `span` comes from a separate crate, it'll be `DUMMY_SP`. Treat it as `None` so
893        // diagnostics use a `note` instead of a `span_label`.
894        match self {
895            DynCompatibilityViolation::SupertraitSelf(spans)
896            | DynCompatibilityViolation::SizedSelf(spans)
897            | DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans)
898            | DynCompatibilityViolation::SupertraitConst(spans) => spans.clone(),
899            DynCompatibilityViolation::AssocConst(_, span)
900            | DynCompatibilityViolation::GAT(_, span)
901            | DynCompatibilityViolation::Method(_, _, span) => {
902                if *span != DUMMY_SP {
903                    smallvec![*span]
904                } else {
905                    smallvec![]
906                }
907            }
908        }
909    }
910}
911
912#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
913pub enum DynCompatibilityViolationSolution {
914    None,
915    AddSelfOrMakeSized {
916        name: Symbol,
917        add_self_sugg: (String, Span),
918        make_sized_sugg: (String, Span),
919    },
920    ChangeToRefSelf(Symbol, Span),
921    MoveToAnotherTrait(Symbol),
922}
923
924impl DynCompatibilityViolationSolution {
925    pub fn add_to<G: EmissionGuarantee>(self, err: &mut Diag<'_, G>) {
926        match self {
927            DynCompatibilityViolationSolution::None => {}
928            DynCompatibilityViolationSolution::AddSelfOrMakeSized {
929                name,
930                add_self_sugg,
931                make_sized_sugg,
932            } => {
933                err.span_suggestion(
934                    add_self_sugg.1,
935                    format!(
936                        "consider turning `{name}` into a method by giving it a `&self` argument"
937                    ),
938                    add_self_sugg.0,
939                    Applicability::MaybeIncorrect,
940                );
941                err.span_suggestion(
942                    make_sized_sugg.1,
943                    format!(
944                        "alternatively, consider constraining `{name}` so it does not apply to \
945                             trait objects"
946                    ),
947                    make_sized_sugg.0,
948                    Applicability::MaybeIncorrect,
949                );
950            }
951            DynCompatibilityViolationSolution::ChangeToRefSelf(name, span) => {
952                err.span_suggestion(
953                    span,
954                    format!("consider changing method `{name}`'s `self` parameter to be `&self`"),
955                    "&Self",
956                    Applicability::MachineApplicable,
957                );
958            }
959            DynCompatibilityViolationSolution::MoveToAnotherTrait(name) => {
960                err.help(format!("consider moving `{name}` to another trait"));
961            }
962        }
963    }
964}
965
966/// Reasons a method might not be dyn-compatible.
967#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
968pub enum MethodViolationCode {
969    /// e.g., `fn foo()`
970    StaticMethod(Option<(/* add &self */ (String, Span), /* add Self: Sized */ (String, Span))>),
971
972    /// e.g., `fn foo(&self, x: Self)`
973    ReferencesSelfInput(Option<Span>),
974
975    /// e.g., `fn foo(&self) -> Self`
976    ReferencesSelfOutput,
977
978    /// e.g., `fn foo(&self) -> impl Sized`
979    ReferencesImplTraitInTrait(Span),
980
981    /// e.g., `async fn foo(&self)`
982    AsyncFn,
983
984    /// e.g., `fn foo(&self) where Self: Clone`
985    WhereClauseReferencesSelf,
986
987    /// e.g., `fn foo<A>()`
988    Generic,
989
990    /// e.g., `fn (mut ap: ...)`
991    CVariadic,
992
993    /// the method's receiver (`self` argument) can't be dispatched on
994    UndispatchableReceiver(Option<Span>),
995}
996
997/// These are the error cases for `codegen_select_candidate`.
998#[derive(Copy, Clone, Debug, Hash, HashStable, Encodable, Decodable)]
999pub enum CodegenObligationError {
1000    /// Ambiguity can happen when monomorphizing during trans
1001    /// expands to some humongous type that never occurred
1002    /// statically -- this humongous type can then overflow,
1003    /// leading to an ambiguous result. So report this as an
1004    /// overflow bug, since I believe this is the only case
1005    /// where ambiguity can result.
1006    Ambiguity,
1007    /// This can trigger when we have a global bound that is not actually satisfied
1008    /// due to trivial bounds.
1009    Unimplemented,
1010    /// The selected impl has unconstrained generic parameters. This will emit an error
1011    /// during impl WF checking.
1012    UnconstrainedParam(ErrorGuaranteed),
1013}