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
417#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
420pub enum IsConstable {
421 No,
422 Fn,
424 Ctor,
426}
427
428#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)]
433#[derive(TypeVisitable, TypeFoldable)]
434pub enum WellFormedLoc {
435 Ty(LocalDefId),
437 Param {
441 function: LocalDefId,
443 param_idx: usize,
447 },
448}
449
450impl<'tcx> ObligationCauseCode<'tcx> {
451 pub fn peel_derives(&self) -> &Self {
453 let mut base_cause = self;
454 while let Some(parent_code) = base_cause.parent() {
455 base_cause = parent_code;
456 }
457 base_cause
458 }
459
460 pub fn parent(&self) -> Option<&Self> {
461 match self {
462 ObligationCauseCode::FunctionArg { parent_code, .. } => Some(parent_code),
463 ObligationCauseCode::BuiltinDerived(derived)
464 | ObligationCauseCode::WellFormedDerived(derived)
465 | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
466 Some(&derived.parent_code)
467 }
468 ObligationCauseCode::BuiltinDerivedHost(derived)
469 | ObligationCauseCode::ImplDerivedHost(box ImplDerivedHostCause { derived, .. }) => {
470 Some(&derived.parent_code)
471 }
472 _ => None,
473 }
474 }
475
476 pub fn peel_derives_with_predicate(&self) -> (&Self, Option<ty::PolyTraitPredicate<'tcx>>) {
479 let mut base_cause = self;
480 let mut base_trait_pred = None;
481 while let Some((parent_code, parent_pred)) = base_cause.parent_with_predicate() {
482 base_cause = parent_code;
483 if let Some(parent_pred) = parent_pred {
484 base_trait_pred = Some(parent_pred);
485 }
486 }
487
488 (base_cause, base_trait_pred)
489 }
490
491 pub fn parent_with_predicate(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
492 match self {
493 ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)),
494 ObligationCauseCode::BuiltinDerived(derived)
495 | ObligationCauseCode::WellFormedDerived(derived)
496 | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => {
497 Some((&derived.parent_code, Some(derived.parent_trait_pred)))
498 }
499 _ => None,
500 }
501 }
502
503 pub fn peel_match_impls(&self) -> &Self {
504 match self {
505 ObligationCauseCode::MatchImpl(cause, _) => cause.code(),
506 _ => self,
507 }
508 }
509}
510
511#[cfg(target_pointer_width = "64")]
513rustc_data_structures::static_assert_size!(ObligationCauseCode<'_>, 48);
514
515#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
516#[derive(TypeVisitable, TypeFoldable)]
517pub struct MatchExpressionArmCause<'tcx> {
518 pub arm_block_id: Option<HirId>,
519 pub arm_ty: Ty<'tcx>,
520 pub arm_span: Span,
521 pub prior_arm_block_id: Option<HirId>,
522 pub prior_arm_ty: Ty<'tcx>,
523 pub prior_arm_span: Span,
524 pub scrut_span: Span,
526 pub source: hir::MatchSource,
528 pub expr_span: Span,
530 pub prior_non_diverging_arms: Vec<Span>,
534 pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
536}
537
538#[derive(Copy, Clone, Debug, PartialEq, Eq)]
542#[derive(TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
543pub struct PatternOriginExpr {
544 pub peeled_span: Span,
550 pub peeled_count: usize,
552 pub peeled_prefix_suggestion_parentheses: bool,
555}
556
557#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
558#[derive(TypeVisitable, TypeFoldable)]
559pub struct DerivedCause<'tcx> {
560 pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
565
566 pub parent_code: ObligationCauseCodeHandle<'tcx>,
568}
569
570#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
571#[derive(TypeVisitable, TypeFoldable)]
572pub struct ImplDerivedCause<'tcx> {
573 pub derived: DerivedCause<'tcx>,
574 pub impl_or_alias_def_id: DefId,
579 pub impl_def_predicate_index: Option<usize>,
581 pub span: Span,
582}
583
584#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
585#[derive(TypeVisitable, TypeFoldable)]
586pub struct DerivedHostCause<'tcx> {
587 pub parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
592
593 pub parent_code: ObligationCauseCodeHandle<'tcx>,
595}
596
597#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
598#[derive(TypeVisitable, TypeFoldable)]
599pub struct ImplDerivedHostCause<'tcx> {
600 pub derived: DerivedHostCause<'tcx>,
601 pub impl_def_id: DefId,
603 pub span: Span,
604}
605
606#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
607pub enum SelectionError<'tcx> {
608 Unimplemented,
610 SignatureMismatch(Box<SignatureMismatchData<'tcx>>),
614 TraitDynIncompatible(DefId),
616 NotConstEvaluatable(NotConstEvaluatable),
618 Overflow(OverflowError),
620 OpaqueTypeAutoTraitLeakageUnknown(DefId),
624 ConstArgHasWrongType { ct: ty::Const<'tcx>, ct_ty: Ty<'tcx>, expected_ty: Ty<'tcx> },
626}
627
628#[derive(Clone, Debug, PartialEq, Eq, TypeVisitable)]
629pub struct SignatureMismatchData<'tcx> {
630 pub found_trait_ref: ty::TraitRef<'tcx>,
631 pub expected_trait_ref: ty::TraitRef<'tcx>,
632 pub terr: ty::error::TypeError<'tcx>,
633}
634
635pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
643
644#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
674#[derive(TypeFoldable, TypeVisitable)]
675pub enum ImplSource<'tcx, N> {
676 UserDefined(ImplSourceUserDefinedData<'tcx, N>),
678
679 Param(ThinVec<N>),
684
685 Builtin(BuiltinImplSource, ThinVec<N>),
687}
688
689impl<'tcx, N> ImplSource<'tcx, N> {
690 pub fn nested_obligations(self) -> ThinVec<N> {
691 match self {
692 ImplSource::UserDefined(i) => i.nested,
693 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
694 }
695 }
696
697 pub fn borrow_nested_obligations(&self) -> &[N] {
698 match self {
699 ImplSource::UserDefined(i) => &i.nested,
700 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
701 }
702 }
703
704 pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
705 match self {
706 ImplSource::UserDefined(i) => &mut i.nested,
707 ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
708 }
709 }
710
711 pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
712 where
713 F: FnMut(N) -> M,
714 {
715 match self {
716 ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData {
717 impl_def_id: i.impl_def_id,
718 args: i.args,
719 nested: i.nested.into_iter().map(f).collect(),
720 }),
721 ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
722 ImplSource::Builtin(source, n) => {
723 ImplSource::Builtin(source, n.into_iter().map(f).collect())
724 }
725 }
726 }
727}
728
729#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
740#[derive(TypeFoldable, TypeVisitable)]
741pub struct ImplSourceUserDefinedData<'tcx, N> {
742 pub impl_def_id: DefId,
743 pub args: GenericArgsRef<'tcx>,
744 pub nested: ThinVec<N>,
745}
746
747#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
748pub enum DynCompatibilityViolation {
749 SizedSelf(SmallVec<[Span; 1]>),
751
752 SupertraitSelf(SmallVec<[Span; 1]>),
755
756 SupertraitNonLifetimeBinder(SmallVec<[Span; 1]>),
758
759 Method(Symbol, MethodViolationCode, Span),
761
762 AssocConst(Symbol, Span),
764
765 GAT(Symbol, Span),
767}
768
769impl DynCompatibilityViolation {
770 pub fn error_msg(&self) -> Cow<'static, str> {
771 match self {
772 DynCompatibilityViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
773 DynCompatibilityViolation::SupertraitSelf(spans) => {
774 if spans.iter().any(|sp| *sp != DUMMY_SP) {
775 "it uses `Self` as a type parameter".into()
776 } else {
777 "it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
778 .into()
779 }
780 }
781 DynCompatibilityViolation::SupertraitNonLifetimeBinder(_) => {
782 "where clause cannot reference non-lifetime `for<...>` variables".into()
783 }
784 DynCompatibilityViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => {
785 format!("associated function `{name}` has no `self` parameter").into()
786 }
787 DynCompatibilityViolation::Method(
788 name,
789 MethodViolationCode::ReferencesSelfInput(_),
790 DUMMY_SP,
791 ) => format!("method `{name}` references the `Self` type in its parameters").into(),
792 DynCompatibilityViolation::Method(
793 name,
794 MethodViolationCode::ReferencesSelfInput(_),
795 _,
796 ) => format!("method `{name}` references the `Self` type in this parameter").into(),
797 DynCompatibilityViolation::Method(
798 name,
799 MethodViolationCode::ReferencesSelfOutput,
800 _,
801 ) => format!("method `{name}` references the `Self` type in its return type").into(),
802 DynCompatibilityViolation::Method(
803 name,
804 MethodViolationCode::ReferencesImplTraitInTrait(_),
805 _,
806 ) => {
807 format!("method `{name}` references an `impl Trait` type in its return type").into()
808 }
809 DynCompatibilityViolation::Method(name, MethodViolationCode::AsyncFn, _) => {
810 format!("method `{name}` is `async`").into()
811 }
812 DynCompatibilityViolation::Method(
813 name,
814 MethodViolationCode::WhereClauseReferencesSelf,
815 _,
816 ) => format!("method `{name}` references the `Self` type in its `where` clause").into(),
817 DynCompatibilityViolation::Method(name, MethodViolationCode::Generic, _) => {
818 format!("method `{name}` has generic type parameters").into()
819 }
820 DynCompatibilityViolation::Method(
821 name,
822 MethodViolationCode::UndispatchableReceiver(_),
823 _,
824 ) => format!("method `{name}`'s `self` parameter cannot be dispatched on").into(),
825 DynCompatibilityViolation::AssocConst(name, DUMMY_SP) => {
826 format!("it contains associated `const` `{name}`").into()
827 }
828 DynCompatibilityViolation::AssocConst(..) => {
829 "it contains this associated `const`".into()
830 }
831 DynCompatibilityViolation::GAT(name, _) => {
832 format!("it contains the generic associated type `{name}`").into()
833 }
834 }
835 }
836
837 pub fn solution(&self) -> DynCompatibilityViolationSolution {
838 match self {
839 DynCompatibilityViolation::SizedSelf(_)
840 | DynCompatibilityViolation::SupertraitSelf(_)
841 | DynCompatibilityViolation::SupertraitNonLifetimeBinder(..) => {
842 DynCompatibilityViolationSolution::None
843 }
844 DynCompatibilityViolation::Method(
845 name,
846 MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
847 _,
848 ) => DynCompatibilityViolationSolution::AddSelfOrMakeSized {
849 name: *name,
850 add_self_sugg: add_self_sugg.clone(),
851 make_sized_sugg: make_sized_sugg.clone(),
852 },
853 DynCompatibilityViolation::Method(
854 name,
855 MethodViolationCode::UndispatchableReceiver(Some(span)),
856 _,
857 ) => DynCompatibilityViolationSolution::ChangeToRefSelf(*name, *span),
858 DynCompatibilityViolation::AssocConst(name, _)
859 | DynCompatibilityViolation::GAT(name, _)
860 | DynCompatibilityViolation::Method(name, ..) => {
861 DynCompatibilityViolationSolution::MoveToAnotherTrait(*name)
862 }
863 }
864 }
865
866 pub fn spans(&self) -> SmallVec<[Span; 1]> {
867 match self {
870 DynCompatibilityViolation::SupertraitSelf(spans)
871 | DynCompatibilityViolation::SizedSelf(spans)
872 | DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans) => spans.clone(),
873 DynCompatibilityViolation::AssocConst(_, span)
874 | DynCompatibilityViolation::GAT(_, span)
875 | DynCompatibilityViolation::Method(_, _, span)
876 if *span != DUMMY_SP =>
877 {
878 smallvec![*span]
879 }
880 _ => smallvec![],
881 }
882 }
883}
884
885#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
886pub enum DynCompatibilityViolationSolution {
887 None,
888 AddSelfOrMakeSized {
889 name: Symbol,
890 add_self_sugg: (String, Span),
891 make_sized_sugg: (String, Span),
892 },
893 ChangeToRefSelf(Symbol, Span),
894 MoveToAnotherTrait(Symbol),
895}
896
897impl DynCompatibilityViolationSolution {
898 pub fn add_to<G: EmissionGuarantee>(self, err: &mut Diag<'_, G>) {
899 match self {
900 DynCompatibilityViolationSolution::None => {}
901 DynCompatibilityViolationSolution::AddSelfOrMakeSized {
902 name,
903 add_self_sugg,
904 make_sized_sugg,
905 } => {
906 err.span_suggestion(
907 add_self_sugg.1,
908 format!(
909 "consider turning `{name}` into a method by giving it a `&self` argument"
910 ),
911 add_self_sugg.0,
912 Applicability::MaybeIncorrect,
913 );
914 err.span_suggestion(
915 make_sized_sugg.1,
916 format!(
917 "alternatively, consider constraining `{name}` so it does not apply to \
918 trait objects"
919 ),
920 make_sized_sugg.0,
921 Applicability::MaybeIncorrect,
922 );
923 }
924 DynCompatibilityViolationSolution::ChangeToRefSelf(name, span) => {
925 err.span_suggestion(
926 span,
927 format!("consider changing method `{name}`'s `self` parameter to be `&self`"),
928 "&Self",
929 Applicability::MachineApplicable,
930 );
931 }
932 DynCompatibilityViolationSolution::MoveToAnotherTrait(name) => {
933 err.help(format!("consider moving `{name}` to another trait"));
934 }
935 }
936 }
937}
938
939#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
941pub enum MethodViolationCode {
942 StaticMethod(Option<((String, Span), (String, Span))>),
944
945 ReferencesSelfInput(Option<Span>),
947
948 ReferencesSelfOutput,
950
951 ReferencesImplTraitInTrait(Span),
953
954 AsyncFn,
956
957 WhereClauseReferencesSelf,
959
960 Generic,
962
963 UndispatchableReceiver(Option<Span>),
965}
966
967#[derive(Copy, Clone, Debug, Hash, HashStable, Encodable, Decodable)]
969pub enum CodegenObligationError {
970 Ambiguity,
977 Unimplemented,
980 UnconstrainedParam(ErrorGuaranteed),
983}