1pub 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#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
42#[derive(TypeVisitable, TypeFoldable)]
43pub struct ObligationCause<'tcx> {
44 pub span: Span,
45
46 pub body_id: LocalDefId,
53
54 code: ObligationCauseCodeHandle<'tcx>,
55}
56
57impl 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 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#[derive(Clone, PartialEq, Eq, Default, HashStable)]
149#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
150pub struct ObligationCauseCodeHandle<'tcx> {
151 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 Misc,
185
186 SliceOrArrayElem,
188
189 ArrayLen(Ty<'tcx>),
191
192 TupleElem,
194
195 WhereClause(DefId, Span),
198
199 OpaqueTypeBound(Span, Option<LocalDefId>),
203
204 WhereClauseInExpr(DefId, Span, HirId, usize),
209
210 HostEffectInExpr(DefId, Span, HirId, usize),
213
214 ReferenceOutlivesReferent(Ty<'tcx>),
216
217 ObjectTypeBound(Ty<'tcx>, ty::Region<'tcx>),
219
220 Coercion {
222 source: Ty<'tcx>,
223 target: Ty<'tcx>,
224 },
225
226 AssignmentLhsSized,
229 TupleInitializerSized,
231 StructInitializerSized,
233 VariableType(HirId),
235 SizedArgumentType(Option<HirId>),
237 SizedReturnType,
239 SizedCallReturnType,
241 SizedYieldType,
243 InlineAsmSized,
245 SizedClosureCapture(LocalDefId),
247 SizedCoroutineInterior(LocalDefId),
249 RepeatElementCopy {
251 is_constable: IsConstable,
254
255 elt_span: Span,
259 },
260
261 FieldSized {
263 adt_kind: AdtKind,
264 span: Span,
265 last: bool,
266 },
267
268 SizedConstOrStatic,
270
271 SharedStatic,
273
274 BuiltinDerived(DerivedCause<'tcx>),
277
278 ImplDerived(Box<ImplDerivedCause<'tcx>>),
281
282 WellFormedDerived(DerivedCause<'tcx>),
284
285 ImplDerivedHost(Box<ImplDerivedHostCause<'tcx>>),
288
289 BuiltinDerivedHost(DerivedHostCause<'tcx>),
292
293 FunctionArg {
296 arg_hir_id: HirId,
298 call_hir_id: HirId,
300 parent_code: ObligationCauseCodeHandle<'tcx>,
302 },
303
304 CompareImplItem {
307 impl_item_def_id: LocalDefId,
308 trait_item_def_id: DefId,
309 kind: ty::AssocKind,
310 },
311
312 CheckAssociatedTypeBounds {
314 impl_item_def_id: LocalDefId,
315 trait_item_def_id: DefId,
316 },
317
318 ExprAssignable,
320
321 MatchExpressionArm(Box<MatchExpressionArmCause<'tcx>>),
323
324 Pattern {
326 span: Option<Span>,
328 root_ty: Ty<'tcx>,
330 origin_expr: Option<PatternOriginExpr>,
332 },
333
334 IfExpression {
336 expr_id: HirId,
337 tail_defines_return_position_impl_trait: Option<LocalDefId>,
339 },
340
341 IfExpressionWithNoElse,
343
344 MainFunctionType,
346
347 LangFunctionType(Symbol),
349
350 IntrinsicType,
352
353 LetElse,
355
356 MethodReceiver,
358
359 ReturnNoExpression,
361
362 ReturnValue(HirId),
364
365 OpaqueReturnType(Option<(Ty<'tcx>, HirId)>),
367
368 BlockTailExpression(HirId, hir::MatchSource),
370
371 TrivialBound,
373
374 AwaitableExpr(HirId),
375
376 ForLoopIterator,
377
378 QuestionMark,
379
380 WellFormed(Option<WellFormedLoc>),
387
388 MatchImpl(ObligationCause<'tcx>, DefId),
391
392 BinOp {
393 lhs_hir_id: HirId,
394 rhs_hir_id: Option<HirId>,
395 rhs_span: Option<Span>,
396 rhs_is_lit: bool,
397 output_ty: Option<Ty<'tcx>>,
398 },
399
400 AscribeUserTypeProvePredicate(Span),
401
402 RustCall,
403
404 DynCompatible(Span),
405
406 AlwaysApplicableImpl,
409
410 ConstParam(Ty<'tcx>),
412
413 TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
415
416 UnsizedNonPlaceExpr(Span),
419}
420
421#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
424pub enum IsConstable {
425 No,
426 Fn,
428 Ctor,
430}
431
432#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)]
437#[derive(TypeVisitable, TypeFoldable)]
438pub enum WellFormedLoc {
439 Ty(LocalDefId),
441 Param {
445 function: LocalDefId,
447 param_idx: usize,
451 },
452}
453
454impl<'tcx> ObligationCauseCode<'tcx> {
455 pub fn peel_derives(&self) -> &Self {
457 let mut base_cause = self;
458 while let Some(parent_code) = base_cause.parent() {
459 base_cause = parent_code;
460 }
461 base_cause
462 }
463
464 pub fn parent(&self) -> Option<&Self> {
465 match self {
466 ObligationCauseCode::FunctionArg { parent_code, .. } => Some(parent_code),
467 ObligationCauseCode::BuiltinDerived(derived)
468 | ObligationCauseCode::WellFormedDerived(derived)
469 | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
470 Some(&derived.parent_code)
471 }
472 ObligationCauseCode::BuiltinDerivedHost(derived)
473 | ObligationCauseCode::ImplDerivedHost(box ImplDerivedHostCause { derived, .. }) => {
474 Some(&derived.parent_code)
475 }
476 _ => None,
477 }
478 }
479
480 pub fn peel_derives_with_predicate(&self) -> (&Self, Option<ty::PolyTraitPredicate<'tcx>>) {
483 let mut base_cause = self;
484 let mut base_trait_pred = None;
485 while let Some((parent_code, parent_pred)) = base_cause.parent_with_predicate() {
486 base_cause = parent_code;
487 if let Some(parent_pred) = parent_pred {
488 base_trait_pred = Some(parent_pred);
489 }
490 }
491
492 (base_cause, base_trait_pred)
493 }
494
495 pub fn parent_with_predicate(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
496 match self {
497 ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)),
498 ObligationCauseCode::BuiltinDerived(derived)
499 | ObligationCauseCode::WellFormedDerived(derived)
500 | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
501 Some((&derived.parent_code, Some(derived.parent_trait_pred)))
502 }
503 _ => None,
504 }
505 }
506
507 pub fn peel_match_impls(&self) -> &Self {
508 match self {
509 ObligationCauseCode::MatchImpl(cause, _) => cause.code(),
510 _ => self,
511 }
512 }
513}
514
515#[cfg(target_pointer_width = "64")]
517rustc_data_structures::static_assert_size!(ObligationCauseCode<'_>, 48);
518
519#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
520#[derive(TypeVisitable, TypeFoldable)]
521pub struct MatchExpressionArmCause<'tcx> {
522 pub arm_block_id: Option<HirId>,
523 pub arm_ty: Ty<'tcx>,
524 pub arm_span: Span,
525 pub prior_arm_block_id: Option<HirId>,
526 pub prior_arm_ty: Ty<'tcx>,
527 pub prior_arm_span: Span,
528 pub scrut_span: Span,
530 pub source: hir::MatchSource,
532 pub expr_span: Span,
534 pub prior_non_diverging_arms: Vec<Span>,
538 pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
540}
541
542#[derive(Copy, Clone, Debug, PartialEq, Eq)]
546#[derive(TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
547pub struct PatternOriginExpr {
548 pub peeled_span: Span,
554 pub peeled_count: usize,
556 pub peeled_prefix_suggestion_parentheses: bool,
559}
560
561#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
562#[derive(TypeVisitable, TypeFoldable)]
563pub struct DerivedCause<'tcx> {
564 pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
569
570 pub parent_code: ObligationCauseCodeHandle<'tcx>,
572}
573
574#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
575#[derive(TypeVisitable, TypeFoldable)]
576pub struct ImplDerivedCause<'tcx> {
577 pub derived: DerivedCause<'tcx>,
578 pub impl_or_alias_def_id: DefId,
583 pub impl_def_predicate_index: Option<usize>,
585 pub span: Span,
586}
587
588#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
589#[derive(TypeVisitable, TypeFoldable)]
590pub struct DerivedHostCause<'tcx> {
591 pub parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
596
597 pub parent_code: ObligationCauseCodeHandle<'tcx>,
599}
600
601#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
602#[derive(TypeVisitable, TypeFoldable)]
603pub struct ImplDerivedHostCause<'tcx> {
604 pub derived: DerivedHostCause<'tcx>,
605 pub impl_def_id: DefId,
607 pub span: Span,
608}
609
610#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
611pub enum SelectionError<'tcx> {
612 Unimplemented,
614 SignatureMismatch(Box<SignatureMismatchData<'tcx>>),
618 TraitDynIncompatible(DefId),
620 NotConstEvaluatable(NotConstEvaluatable),
622 Overflow(OverflowError),
624 OpaqueTypeAutoTraitLeakageUnknown(DefId),
628 ConstArgHasWrongType { ct: ty::Const<'tcx>, ct_ty: Ty<'tcx>, expected_ty: Ty<'tcx> },
630}
631
632#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
633pub struct SignatureMismatchData<'tcx> {
634 pub found_trait_ref: ty::TraitRef<'tcx>,
635 pub expected_trait_ref: ty::TraitRef<'tcx>,
636 pub terr: ty::error::TypeError<'tcx>,
637}
638
639pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
647
648#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
678#[derive(TypeFoldable, TypeVisitable)]
679pub enum ImplSource<'tcx, N> {
680 UserDefined(ImplSourceUserDefinedData<'tcx, N>),
682
683 Param(ThinVec<N>),
688
689 Builtin(BuiltinImplSource, ThinVec<N>),
691}
692
693impl<'tcx, N> ImplSource<'tcx, N> {
694 pub fn nested_obligations(self) -> ThinVec<N> {
695 match self {
696 ImplSource::UserDefined(i) => i.nested,
697 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
698 }
699 }
700
701 pub fn borrow_nested_obligations(&self) -> &[N] {
702 match self {
703 ImplSource::UserDefined(i) => &i.nested,
704 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
705 }
706 }
707
708 pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
709 match self {
710 ImplSource::UserDefined(i) => &mut i.nested,
711 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
712 }
713 }
714
715 pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
716 where
717 F: FnMut(N) -> M,
718 {
719 match self {
720 ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData {
721 impl_def_id: i.impl_def_id,
722 args: i.args,
723 nested: i.nested.into_iter().map(f).collect(),
724 }),
725 ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
726 ImplSource::Builtin(source, n) => {
727 ImplSource::Builtin(source, n.into_iter().map(f).collect())
728 }
729 }
730 }
731}
732
733#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
744#[derive(TypeFoldable, TypeVisitable)]
745pub struct ImplSourceUserDefinedData<'tcx, N> {
746 pub impl_def_id: DefId,
747 pub args: GenericArgsRef<'tcx>,
748 pub nested: ThinVec<N>,
749}
750
751#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
752pub enum DynCompatibilityViolation {
753 SizedSelf(SmallVec<[Span; 1]>),
755
756 SupertraitSelf(SmallVec<[Span; 1]>),
759
760 SupertraitNonLifetimeBinder(SmallVec<[Span; 1]>),
762
763 SupertraitConst(SmallVec<[Span; 1]>),
765
766 Method(Symbol, MethodViolationCode, Span),
768
769 AssocConst(Symbol, Span),
771
772 GAT(Symbol, Span),
774}
775
776impl DynCompatibilityViolation {
777 pub fn error_msg(&self) -> Cow<'static, str> {
778 match self {
779 DynCompatibilityViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
780 DynCompatibilityViolation::SupertraitSelf(spans) => {
781 if spans.iter().any(|sp| *sp != DUMMY_SP) {
782 "it uses `Self` as a type parameter".into()
783 } else {
784 "it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
785 .into()
786 }
787 }
788 DynCompatibilityViolation::SupertraitNonLifetimeBinder(_) => {
789 "where clause cannot reference non-lifetime `for<...>` variables".into()
790 }
791 DynCompatibilityViolation::SupertraitConst(_) => {
792 "it cannot have a `const` supertrait".into()
793 }
794 DynCompatibilityViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => {
795 format!("associated function `{name}` has no `self` parameter").into()
796 }
797 DynCompatibilityViolation::Method(
798 name,
799 MethodViolationCode::ReferencesSelfInput(_),
800 DUMMY_SP,
801 ) => format!("method `{name}` references the `Self` type in its parameters").into(),
802 DynCompatibilityViolation::Method(
803 name,
804 MethodViolationCode::ReferencesSelfInput(_),
805 _,
806 ) => format!("method `{name}` references the `Self` type in this parameter").into(),
807 DynCompatibilityViolation::Method(
808 name,
809 MethodViolationCode::ReferencesSelfOutput,
810 _,
811 ) => format!("method `{name}` references the `Self` type in its return type").into(),
812 DynCompatibilityViolation::Method(
813 name,
814 MethodViolationCode::ReferencesImplTraitInTrait(_),
815 _,
816 ) => {
817 format!("method `{name}` references an `impl Trait` type in its return type").into()
818 }
819 DynCompatibilityViolation::Method(name, MethodViolationCode::AsyncFn, _) => {
820 format!("method `{name}` is `async`").into()
821 }
822 DynCompatibilityViolation::Method(
823 name,
824 MethodViolationCode::WhereClauseReferencesSelf,
825 _,
826 ) => format!("method `{name}` references the `Self` type in its `where` clause").into(),
827 DynCompatibilityViolation::Method(name, MethodViolationCode::Generic, _) => {
828 format!("method `{name}` has generic type parameters").into()
829 }
830 DynCompatibilityViolation::Method(
831 name,
832 MethodViolationCode::UndispatchableReceiver(_),
833 _,
834 ) => format!("method `{name}`'s `self` parameter cannot be dispatched on").into(),
835 DynCompatibilityViolation::AssocConst(name, DUMMY_SP) => {
836 format!("it contains associated `const` `{name}`").into()
837 }
838 DynCompatibilityViolation::AssocConst(..) => {
839 "it contains this associated `const`".into()
840 }
841 DynCompatibilityViolation::GAT(name, _) => {
842 format!("it contains the generic associated type `{name}`").into()
843 }
844 }
845 }
846
847 pub fn solution(&self) -> DynCompatibilityViolationSolution {
848 match self {
849 DynCompatibilityViolation::SizedSelf(_)
850 | DynCompatibilityViolation::SupertraitSelf(_)
851 | DynCompatibilityViolation::SupertraitNonLifetimeBinder(..)
852 | DynCompatibilityViolation::SupertraitConst(_) => {
853 DynCompatibilityViolationSolution::None
854 }
855 DynCompatibilityViolation::Method(
856 name,
857 MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
858 _,
859 ) => DynCompatibilityViolationSolution::AddSelfOrMakeSized {
860 name: *name,
861 add_self_sugg: add_self_sugg.clone(),
862 make_sized_sugg: make_sized_sugg.clone(),
863 },
864 DynCompatibilityViolation::Method(
865 name,
866 MethodViolationCode::UndispatchableReceiver(Some(span)),
867 _,
868 ) => DynCompatibilityViolationSolution::ChangeToRefSelf(*name, *span),
869 DynCompatibilityViolation::AssocConst(name, _)
870 | DynCompatibilityViolation::GAT(name, _)
871 | DynCompatibilityViolation::Method(name, ..) => {
872 DynCompatibilityViolationSolution::MoveToAnotherTrait(*name)
873 }
874 }
875 }
876
877 pub fn spans(&self) -> SmallVec<[Span; 1]> {
878 match self {
881 DynCompatibilityViolation::SupertraitSelf(spans)
882 | DynCompatibilityViolation::SizedSelf(spans)
883 | DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans)
884 | DynCompatibilityViolation::SupertraitConst(spans) => spans.clone(),
885 DynCompatibilityViolation::AssocConst(_, span)
886 | DynCompatibilityViolation::GAT(_, span)
887 | DynCompatibilityViolation::Method(_, _, span) => {
888 if *span != DUMMY_SP {
889 smallvec![*span]
890 } else {
891 smallvec![]
892 }
893 }
894 }
895 }
896}
897
898#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
899pub enum DynCompatibilityViolationSolution {
900 None,
901 AddSelfOrMakeSized {
902 name: Symbol,
903 add_self_sugg: (String, Span),
904 make_sized_sugg: (String, Span),
905 },
906 ChangeToRefSelf(Symbol, Span),
907 MoveToAnotherTrait(Symbol),
908}
909
910impl DynCompatibilityViolationSolution {
911 pub fn add_to<G: EmissionGuarantee>(self, err: &mut Diag<'_, G>) {
912 match self {
913 DynCompatibilityViolationSolution::None => {}
914 DynCompatibilityViolationSolution::AddSelfOrMakeSized {
915 name,
916 add_self_sugg,
917 make_sized_sugg,
918 } => {
919 err.span_suggestion(
920 add_self_sugg.1,
921 format!(
922 "consider turning `{name}` into a method by giving it a `&self` argument"
923 ),
924 add_self_sugg.0,
925 Applicability::MaybeIncorrect,
926 );
927 err.span_suggestion(
928 make_sized_sugg.1,
929 format!(
930 "alternatively, consider constraining `{name}` so it does not apply to \
931 trait objects"
932 ),
933 make_sized_sugg.0,
934 Applicability::MaybeIncorrect,
935 );
936 }
937 DynCompatibilityViolationSolution::ChangeToRefSelf(name, span) => {
938 err.span_suggestion(
939 span,
940 format!("consider changing method `{name}`'s `self` parameter to be `&self`"),
941 "&Self",
942 Applicability::MachineApplicable,
943 );
944 }
945 DynCompatibilityViolationSolution::MoveToAnotherTrait(name) => {
946 err.help(format!("consider moving `{name}` to another trait"));
947 }
948 }
949 }
950}
951
952#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
954pub enum MethodViolationCode {
955 StaticMethod(Option<((String, Span), (String, Span))>),
957
958 ReferencesSelfInput(Option<Span>),
960
961 ReferencesSelfOutput,
963
964 ReferencesImplTraitInTrait(Span),
966
967 AsyncFn,
969
970 WhereClauseReferencesSelf,
972
973 Generic,
975
976 UndispatchableReceiver(Option<Span>),
978}
979
980#[derive(Copy, Clone, Debug, Hash, HashStable, Encodable, Decodable)]
982pub enum CodegenObligationError {
983 Ambiguity,
990 Unimplemented,
993 UnconstrainedParam(ErrorGuaranteed),
996}