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(Box<IfExpressionCause<'tcx>>),
336
337 IfExpressionWithNoElse,
339
340 MainFunctionType,
342
343 LangFunctionType(Symbol),
345
346 IntrinsicType,
348
349 LetElse,
351
352 MethodReceiver,
354
355 ReturnNoExpression,
357
358 ReturnValue(HirId),
360
361 OpaqueReturnType(Option<(Ty<'tcx>, HirId)>),
363
364 BlockTailExpression(HirId, hir::MatchSource),
366
367 TrivialBound,
369
370 AwaitableExpr(HirId),
371
372 ForLoopIterator,
373
374 QuestionMark,
375
376 WellFormed(Option<WellFormedLoc>),
383
384 MatchImpl(ObligationCause<'tcx>, DefId),
387
388 BinOp {
389 lhs_hir_id: HirId,
390 rhs_hir_id: Option<HirId>,
391 rhs_span: Option<Span>,
392 rhs_is_lit: bool,
393 output_ty: Option<Ty<'tcx>>,
394 },
395
396 AscribeUserTypeProvePredicate(Span),
397
398 RustCall,
399
400 AlwaysApplicableImpl,
403
404 ConstParam(Ty<'tcx>),
406
407 TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
409}
410
411#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
414pub enum IsConstable {
415 No,
416 Fn,
418 Ctor,
420}
421
422#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)]
427#[derive(TypeVisitable, TypeFoldable)]
428pub enum WellFormedLoc {
429 Ty(LocalDefId),
431 Param {
435 function: LocalDefId,
437 param_idx: usize,
441 },
442}
443
444impl<'tcx> ObligationCauseCode<'tcx> {
445 pub fn peel_derives(&self) -> &Self {
447 let mut base_cause = self;
448 while let Some(parent_code) = base_cause.parent() {
449 base_cause = parent_code;
450 }
451 base_cause
452 }
453
454 pub fn parent(&self) -> Option<&Self> {
455 match self {
456 ObligationCauseCode::FunctionArg { parent_code, .. } => Some(parent_code),
457 ObligationCauseCode::BuiltinDerived(derived)
458 | ObligationCauseCode::WellFormedDerived(derived)
459 | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
460 Some(&derived.parent_code)
461 }
462 ObligationCauseCode::BuiltinDerivedHost(derived)
463 | ObligationCauseCode::ImplDerivedHost(box ImplDerivedHostCause { derived, .. }) => {
464 Some(&derived.parent_code)
465 }
466 _ => None,
467 }
468 }
469
470 pub fn peel_derives_with_predicate(&self) -> (&Self, Option<ty::PolyTraitPredicate<'tcx>>) {
473 let mut base_cause = self;
474 let mut base_trait_pred = None;
475 while let Some((parent_code, parent_pred)) = base_cause.parent_with_predicate() {
476 base_cause = parent_code;
477 if let Some(parent_pred) = parent_pred {
478 base_trait_pred = Some(parent_pred);
479 }
480 }
481
482 (base_cause, base_trait_pred)
483 }
484
485 pub fn parent_with_predicate(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
486 match self {
487 ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)),
488 ObligationCauseCode::BuiltinDerived(derived)
489 | ObligationCauseCode::WellFormedDerived(derived)
490 | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
491 Some((&derived.parent_code, Some(derived.parent_trait_pred)))
492 }
493 _ => None,
494 }
495 }
496
497 pub fn peel_match_impls(&self) -> &Self {
498 match self {
499 ObligationCauseCode::MatchImpl(cause, _) => cause.code(),
500 _ => self,
501 }
502 }
503}
504
505#[cfg(target_pointer_width = "64")]
507rustc_data_structures::static_assert_size!(ObligationCauseCode<'_>, 48);
508
509#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
510#[derive(TypeVisitable, TypeFoldable)]
511pub struct MatchExpressionArmCause<'tcx> {
512 pub arm_block_id: Option<HirId>,
513 pub arm_ty: Ty<'tcx>,
514 pub arm_span: Span,
515 pub prior_arm_block_id: Option<HirId>,
516 pub prior_arm_ty: Ty<'tcx>,
517 pub prior_arm_span: Span,
518 pub scrut_span: Span,
520 pub source: hir::MatchSource,
522 pub expr_span: Span,
524 pub prior_non_diverging_arms: Vec<Span>,
528 pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
530}
531
532#[derive(Copy, Clone, Debug, PartialEq, Eq)]
536#[derive(TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
537pub struct PatternOriginExpr {
538 pub peeled_span: Span,
544 pub peeled_count: usize,
546 pub peeled_prefix_suggestion_parentheses: bool,
549}
550
551#[derive(Copy, Clone, Debug, PartialEq, Eq)]
552#[derive(TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
553pub struct IfExpressionCause<'tcx> {
554 pub then_id: HirId,
555 pub else_id: HirId,
556 pub then_ty: Ty<'tcx>,
557 pub else_ty: Ty<'tcx>,
558 pub outer_span: Option<Span>,
559 pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
561}
562
563#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
564#[derive(TypeVisitable, TypeFoldable)]
565pub struct DerivedCause<'tcx> {
566 pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
571
572 pub parent_code: ObligationCauseCodeHandle<'tcx>,
574}
575
576#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
577#[derive(TypeVisitable, TypeFoldable)]
578pub struct ImplDerivedCause<'tcx> {
579 pub derived: DerivedCause<'tcx>,
580 pub impl_or_alias_def_id: DefId,
585 pub impl_def_predicate_index: Option<usize>,
587 pub span: Span,
588}
589
590#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
591#[derive(TypeVisitable, TypeFoldable)]
592pub struct DerivedHostCause<'tcx> {
593 pub parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
598
599 pub parent_code: ObligationCauseCodeHandle<'tcx>,
601}
602
603#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
604#[derive(TypeVisitable, TypeFoldable)]
605pub struct ImplDerivedHostCause<'tcx> {
606 pub derived: DerivedHostCause<'tcx>,
607 pub impl_def_id: DefId,
609 pub span: Span,
610}
611
612#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
613pub enum SelectionError<'tcx> {
614 Unimplemented,
616 SignatureMismatch(Box<SignatureMismatchData<'tcx>>),
620 TraitDynIncompatible(DefId),
622 NotConstEvaluatable(NotConstEvaluatable),
624 Overflow(OverflowError),
626 OpaqueTypeAutoTraitLeakageUnknown(DefId),
630 ConstArgHasWrongType { ct: ty::Const<'tcx>, ct_ty: Ty<'tcx>, expected_ty: Ty<'tcx> },
632}
633
634#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
635pub struct SignatureMismatchData<'tcx> {
636 pub found_trait_ref: ty::TraitRef<'tcx>,
637 pub expected_trait_ref: ty::TraitRef<'tcx>,
638 pub terr: ty::error::TypeError<'tcx>,
639}
640
641pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
649
650#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
680#[derive(TypeFoldable, TypeVisitable)]
681pub enum ImplSource<'tcx, N> {
682 UserDefined(ImplSourceUserDefinedData<'tcx, N>),
684
685 Param(ThinVec<N>),
690
691 Builtin(BuiltinImplSource, ThinVec<N>),
693}
694
695impl<'tcx, N> ImplSource<'tcx, N> {
696 pub fn nested_obligations(self) -> ThinVec<N> {
697 match self {
698 ImplSource::UserDefined(i) => i.nested,
699 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
700 }
701 }
702
703 pub fn borrow_nested_obligations(&self) -> &[N] {
704 match self {
705 ImplSource::UserDefined(i) => &i.nested,
706 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
707 }
708 }
709
710 pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
711 match self {
712 ImplSource::UserDefined(i) => &mut i.nested,
713 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
714 }
715 }
716
717 pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
718 where
719 F: FnMut(N) -> M,
720 {
721 match self {
722 ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData {
723 impl_def_id: i.impl_def_id,
724 args: i.args,
725 nested: i.nested.into_iter().map(f).collect(),
726 }),
727 ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
728 ImplSource::Builtin(source, n) => {
729 ImplSource::Builtin(source, n.into_iter().map(f).collect())
730 }
731 }
732 }
733}
734
735#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
746#[derive(TypeFoldable, TypeVisitable)]
747pub struct ImplSourceUserDefinedData<'tcx, N> {
748 pub impl_def_id: DefId,
749 pub args: GenericArgsRef<'tcx>,
750 pub nested: ThinVec<N>,
751}
752
753#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
754pub enum DynCompatibilityViolation {
755 SizedSelf(SmallVec<[Span; 1]>),
757
758 SupertraitSelf(SmallVec<[Span; 1]>),
761
762 SupertraitNonLifetimeBinder(SmallVec<[Span; 1]>),
764
765 Method(Symbol, MethodViolationCode, Span),
767
768 AssocConst(Symbol, Span),
770
771 GAT(Symbol, Span),
773}
774
775impl DynCompatibilityViolation {
776 pub fn error_msg(&self) -> Cow<'static, str> {
777 match self {
778 DynCompatibilityViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
779 DynCompatibilityViolation::SupertraitSelf(spans) => {
780 if spans.iter().any(|sp| *sp != DUMMY_SP) {
781 "it uses `Self` as a type parameter".into()
782 } else {
783 "it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
784 .into()
785 }
786 }
787 DynCompatibilityViolation::SupertraitNonLifetimeBinder(_) => {
788 "where clause cannot reference non-lifetime `for<...>` variables".into()
789 }
790 DynCompatibilityViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => {
791 format!("associated function `{name}` has no `self` parameter").into()
792 }
793 DynCompatibilityViolation::Method(
794 name,
795 MethodViolationCode::ReferencesSelfInput(_),
796 DUMMY_SP,
797 ) => format!("method `{name}` references the `Self` type in its parameters").into(),
798 DynCompatibilityViolation::Method(
799 name,
800 MethodViolationCode::ReferencesSelfInput(_),
801 _,
802 ) => format!("method `{name}` references the `Self` type in this parameter").into(),
803 DynCompatibilityViolation::Method(
804 name,
805 MethodViolationCode::ReferencesSelfOutput,
806 _,
807 ) => format!("method `{name}` references the `Self` type in its return type").into(),
808 DynCompatibilityViolation::Method(
809 name,
810 MethodViolationCode::ReferencesImplTraitInTrait(_),
811 _,
812 ) => {
813 format!("method `{name}` references an `impl Trait` type in its return type").into()
814 }
815 DynCompatibilityViolation::Method(name, MethodViolationCode::AsyncFn, _) => {
816 format!("method `{name}` is `async`").into()
817 }
818 DynCompatibilityViolation::Method(
819 name,
820 MethodViolationCode::WhereClauseReferencesSelf,
821 _,
822 ) => format!("method `{name}` references the `Self` type in its `where` clause").into(),
823 DynCompatibilityViolation::Method(name, MethodViolationCode::Generic, _) => {
824 format!("method `{name}` has generic type parameters").into()
825 }
826 DynCompatibilityViolation::Method(
827 name,
828 MethodViolationCode::UndispatchableReceiver(_),
829 _,
830 ) => format!("method `{name}`'s `self` parameter cannot be dispatched on").into(),
831 DynCompatibilityViolation::AssocConst(name, DUMMY_SP) => {
832 format!("it contains associated `const` `{name}`").into()
833 }
834 DynCompatibilityViolation::AssocConst(..) => {
835 "it contains this associated `const`".into()
836 }
837 DynCompatibilityViolation::GAT(name, _) => {
838 format!("it contains the generic associated type `{name}`").into()
839 }
840 }
841 }
842
843 pub fn solution(&self) -> DynCompatibilityViolationSolution {
844 match self {
845 DynCompatibilityViolation::SizedSelf(_)
846 | DynCompatibilityViolation::SupertraitSelf(_)
847 | DynCompatibilityViolation::SupertraitNonLifetimeBinder(..) => {
848 DynCompatibilityViolationSolution::None
849 }
850 DynCompatibilityViolation::Method(
851 name,
852 MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
853 _,
854 ) => DynCompatibilityViolationSolution::AddSelfOrMakeSized {
855 name: *name,
856 add_self_sugg: add_self_sugg.clone(),
857 make_sized_sugg: make_sized_sugg.clone(),
858 },
859 DynCompatibilityViolation::Method(
860 name,
861 MethodViolationCode::UndispatchableReceiver(Some(span)),
862 _,
863 ) => DynCompatibilityViolationSolution::ChangeToRefSelf(*name, *span),
864 DynCompatibilityViolation::AssocConst(name, _)
865 | DynCompatibilityViolation::GAT(name, _)
866 | DynCompatibilityViolation::Method(name, ..) => {
867 DynCompatibilityViolationSolution::MoveToAnotherTrait(*name)
868 }
869 }
870 }
871
872 pub fn spans(&self) -> SmallVec<[Span; 1]> {
873 match self {
876 DynCompatibilityViolation::SupertraitSelf(spans)
877 | DynCompatibilityViolation::SizedSelf(spans)
878 | DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans) => spans.clone(),
879 DynCompatibilityViolation::AssocConst(_, span)
880 | DynCompatibilityViolation::GAT(_, span)
881 | DynCompatibilityViolation::Method(_, _, span)
882 if *span != DUMMY_SP =>
883 {
884 smallvec![*span]
885 }
886 _ => smallvec![],
887 }
888 }
889}
890
891#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
892pub enum DynCompatibilityViolationSolution {
893 None,
894 AddSelfOrMakeSized {
895 name: Symbol,
896 add_self_sugg: (String, Span),
897 make_sized_sugg: (String, Span),
898 },
899 ChangeToRefSelf(Symbol, Span),
900 MoveToAnotherTrait(Symbol),
901}
902
903impl DynCompatibilityViolationSolution {
904 pub fn add_to<G: EmissionGuarantee>(self, err: &mut Diag<'_, G>) {
905 match self {
906 DynCompatibilityViolationSolution::None => {}
907 DynCompatibilityViolationSolution::AddSelfOrMakeSized {
908 name,
909 add_self_sugg,
910 make_sized_sugg,
911 } => {
912 err.span_suggestion(
913 add_self_sugg.1,
914 format!(
915 "consider turning `{name}` into a method by giving it a `&self` argument"
916 ),
917 add_self_sugg.0,
918 Applicability::MaybeIncorrect,
919 );
920 err.span_suggestion(
921 make_sized_sugg.1,
922 format!(
923 "alternatively, consider constraining `{name}` so it does not apply to \
924 trait objects"
925 ),
926 make_sized_sugg.0,
927 Applicability::MaybeIncorrect,
928 );
929 }
930 DynCompatibilityViolationSolution::ChangeToRefSelf(name, span) => {
931 err.span_suggestion(
932 span,
933 format!("consider changing method `{name}`'s `self` parameter to be `&self`"),
934 "&Self",
935 Applicability::MachineApplicable,
936 );
937 }
938 DynCompatibilityViolationSolution::MoveToAnotherTrait(name) => {
939 err.help(format!("consider moving `{name}` to another trait"));
940 }
941 }
942 }
943}
944
945#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
947pub enum MethodViolationCode {
948 StaticMethod(Option<((String, Span), (String, Span))>),
950
951 ReferencesSelfInput(Option<Span>),
953
954 ReferencesSelfOutput,
956
957 ReferencesImplTraitInTrait(Span),
959
960 AsyncFn,
962
963 WhereClauseReferencesSelf,
965
966 Generic,
968
969 UndispatchableReceiver(Option<Span>),
971}
972
973#[derive(Copy, Clone, Debug, Hash, HashStable, Encodable, Decodable)]
975pub enum CodegenObligationError {
976 Ambiguity,
983 Unimplemented,
986 UnconstrainedParam(ErrorGuaranteed),
989}