Skip to content

Commit 71d24da

Browse files
committed
Split into several messages
1 parent 6c19c08 commit 71d24da

File tree

3 files changed

+250
-47
lines changed

3 files changed

+250
-47
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,64 @@ infer_explicit_lifetime_required_sugg = add explicit lifetime `{$named}` to {$id
184184
*[param_type] type
185185
}
186186
187-
infer_actual_impl_expl_expected = {$leading_ellipsis ->
187+
infer_actual_impl_expl_expected_signature_two = {$leading_ellipsis ->
188188
[true] ...
189189
*[false] {""}
190-
}{$kind ->
191-
[signature] closure with signature `{$ty_or_sig}` must implement `{$trait_path}`
192-
[passive] `{$trait_path}` would have to be implemented for the type `{$ty_or_sig}`
193-
*[other] `{$ty_or_sig}` must implement `{$trait_path}`
194-
}{$lt_kind ->
195-
[two] , for any two lifetimes `'{$lifetime_1}` and `'{$lifetime_2}`...
196-
[any] , for any lifetime `'{$lifetime_1}`...
197-
[some] , for some specific lifetime `'{lifetime_1}`...
198-
*[nothing] {""}
199-
}
190+
}closure with signature `{$ty_or_sig}` must implement `{$trait_path}`, for any two lifetimes `'{$lifetime_1}` and `'{$lifetime_2}`...
191+
infer_actual_impl_expl_expected_signature_any = {$leading_ellipsis ->
192+
[true] ...
193+
*[false] {""}
194+
}closure with signature `{$ty_or_sig}` must implement `{$trait_path}`, for any lifetime `'{$lifetime_1}`...
195+
infer_actual_impl_expl_expected_signature_some = {$leading_ellipsis ->
196+
[true] ...
197+
*[false] {""}
198+
}closure with signature `{$ty_or_sig}` must implement `{$trait_path}`, for some specific lifetime `'{lifetime_1}`...
199+
infer_actual_impl_expl_expected_signature_nothing = {$leading_ellipsis ->
200+
[true] ...
201+
*[false] {""}
202+
}closure with signature `{$ty_or_sig}` must implement `{$trait_path}`
203+
infer_actual_impl_expl_expected_passive_two = {$leading_ellipsis ->
204+
[true] ...
205+
*[false] {""}
206+
}`{$trait_path}` would have to be implemented for the type `{$ty_or_sig}`, for any two lifetimes `'{$lifetime_1}` and `'{$lifetime_2}`...
207+
infer_actual_impl_expl_expected_passive_any = {$leading_ellipsis ->
208+
[true] ...
209+
*[false] {""}
210+
}`{$trait_path}` would have to be implemented for the type `{$ty_or_sig}`, for any lifetime `'{$lifetime_1}`...
211+
infer_actual_impl_expl_expected_passive_some = {$leading_ellipsis ->
212+
[true] ...
213+
*[false] {""}
214+
}`{$trait_path}` would have to be implemented for the type `{$ty_or_sig}`, for some specific lifetime `'{lifetime_1}`...
215+
infer_actual_impl_expl_expected_passive_nothing = {$leading_ellipsis ->
216+
[true] ...
217+
*[false] {""}
218+
}`{$trait_path}` would have to be implemented for the type `{$ty_or_sig}`
219+
infer_actual_impl_expl_expected_other_two = {$leading_ellipsis ->
220+
[true] ...
221+
*[false] {""}
222+
}`{$ty_or_sig}` must implement `{$trait_path}`, for any two lifetimes `'{$lifetime_1}` and `'{$lifetime_2}`...
223+
infer_actual_impl_expl_expected_other_any = {$leading_ellipsis ->
224+
[true] ...
225+
*[false] {""}
226+
}`{$ty_or_sig}` must implement `{$trait_path}`, for any lifetime `'{$lifetime_1}`...
227+
infer_actual_impl_expl_expected_other_some = {$leading_ellipsis ->
228+
[true] ...
229+
*[false] {""}
230+
}`{$ty_or_sig}` must implement `{$trait_path}`, for some specific lifetime `'{lifetime_1}`...
231+
infer_actual_impl_expl_expected_other_nothing = {$leading_ellipsis ->
232+
[true] ...
233+
*[false] {""}
234+
}`{$ty_or_sig}` must implement `{$trait_path}`
200235
201-
infer_actual_impl_expl_but_actually = {$kind_2 ->
202-
[implements_trait] ...but it actually implements `{$trait_path_2}`
203-
[implemented_for_ty] ...but `{$trait_path_2}` is actually implemented for the type `{$ty}`
204-
*[ty_implements] ...but `{$ty}` actually implements `{$trait_path_2}`
205-
}{$has_lifetime ->
236+
infer_actual_impl_expl_but_actually_implements_trait = ...but it actually implements `{$trait_path_2}`{$has_lifetime ->
237+
[true] , for some specific lifetime `'{$lifetime}`
238+
*[false] {""}
239+
}
240+
infer_actual_impl_expl_but_actually_implemented_for_ty = ...but `{$trait_path_2}` is actually implemented for the type `{$ty}`{$has_lifetime ->
241+
[true] , for some specific lifetime `'{$lifetime}`
242+
*[false] {""}
243+
}
244+
infer_actual_impl_expl_but_actually_ty_implements = ...but `{$ty}` actually implements `{$trait_path_2}`{$has_lifetime ->
206245
[true] , for some specific lifetime `'{$lifetime}`
207246
*[false] {""}
208247
}

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 160 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,27 +544,179 @@ pub struct ExplicitLifetimeRequired<'a> {
544544

545545
#[derive(Subdiagnostic)]
546546
pub enum ActualImplExplNotes {
547-
// Field names have to be different across all variants
548-
#[note(infer::actual_impl_expl_expected)]
549-
Expected {
547+
// Field names have to be different across Expected* and ButActually variants
548+
#[note(infer::actual_impl_expl_expected_signature_two)]
549+
ExpectedSignatureTwo {
550+
leading_ellipsis: bool,
551+
ty_or_sig: String,
552+
trait_path: String,
553+
lifetime_1: usize,
554+
lifetime_2: usize,
555+
},
556+
#[note(infer::actual_impl_expl_expected_signature_any)]
557+
ExpectedSignatureAny {
558+
leading_ellipsis: bool,
559+
ty_or_sig: String,
560+
trait_path: String,
561+
lifetime_1: usize,
562+
},
563+
#[note(infer::actual_impl_expl_expected_signature_some)]
564+
ExpectedSignatureSome {
565+
leading_ellipsis: bool,
566+
ty_or_sig: String,
567+
trait_path: String,
568+
lifetime_1: usize,
569+
},
570+
#[note(infer::actual_impl_expl_expected_signature_nothing)]
571+
ExpectedSignatureNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
572+
#[note(infer::actual_impl_expl_expected_passive_two)]
573+
ExpectedPassiveTwo {
550574
leading_ellipsis: bool,
551-
kind: &'static str,
552575
ty_or_sig: String,
553576
trait_path: String,
554-
lt_kind: &'static str,
555577
lifetime_1: usize,
556578
lifetime_2: usize,
557579
},
558-
#[note(infer::actual_impl_expl_but_actually)]
559-
ButActually {
560-
kind_2: &'static str,
580+
#[note(infer::actual_impl_expl_expected_passive_any)]
581+
ExpectedPassiveAny {
582+
leading_ellipsis: bool,
583+
ty_or_sig: String,
584+
trait_path: String,
585+
lifetime_1: usize,
586+
},
587+
#[note(infer::actual_impl_expl_expected_passive_some)]
588+
ExpectedPassiveSome {
589+
leading_ellipsis: bool,
590+
ty_or_sig: String,
591+
trait_path: String,
592+
lifetime_1: usize,
593+
},
594+
#[note(infer::actual_impl_expl_expected_passive_nothing)]
595+
ExpectedPassiveNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
596+
#[note(infer::actual_impl_expl_expected_other_two)]
597+
ExpectedOtherTwo {
598+
leading_ellipsis: bool,
599+
ty_or_sig: String,
600+
trait_path: String,
601+
lifetime_1: usize,
602+
lifetime_2: usize,
603+
},
604+
#[note(infer::actual_impl_expl_expected_other_any)]
605+
ExpectedOtherAny {
606+
leading_ellipsis: bool,
607+
ty_or_sig: String,
608+
trait_path: String,
609+
lifetime_1: usize,
610+
},
611+
#[note(infer::actual_impl_expl_expected_other_some)]
612+
ExpectedOtherSome {
613+
leading_ellipsis: bool,
614+
ty_or_sig: String,
615+
trait_path: String,
616+
lifetime_1: usize,
617+
},
618+
#[note(infer::actual_impl_expl_expected_other_nothing)]
619+
ExpectedOtherNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
620+
#[note(infer::actual_impl_expl_but_actually_implements_trait)]
621+
ButActuallyImplementsTrait { trait_path_2: String, has_lifetime: bool, lifetime: usize },
622+
#[note(infer::actual_impl_expl_but_actually_implemented_for_ty)]
623+
ButActuallyImplementedForTy {
624+
trait_path_2: String,
625+
has_lifetime: bool,
626+
lifetime: usize,
627+
ty: String,
628+
},
629+
#[note(infer::actual_impl_expl_but_actually_ty_implements)]
630+
ButActuallyTyImplements {
561631
trait_path_2: String,
562632
has_lifetime: bool,
563633
lifetime: usize,
564634
ty: String,
565635
},
566636
}
567637

638+
pub enum ActualImplExpectedKind {
639+
Signature,
640+
Passive,
641+
Other,
642+
}
643+
644+
pub enum ActualImplExpectedLifetimeKind {
645+
Two,
646+
Any,
647+
Some,
648+
Nothing,
649+
}
650+
651+
impl ActualImplExplNotes {
652+
pub fn new_expected(
653+
kind: ActualImplExpectedKind,
654+
lt_kind: ActualImplExpectedLifetimeKind,
655+
leading_ellipsis: bool,
656+
ty_or_sig: String,
657+
trait_path: String,
658+
lifetime_1: usize,
659+
lifetime_2: usize,
660+
) -> Self {
661+
match (kind, lt_kind) {
662+
(ActualImplExpectedKind::Signature, ActualImplExpectedLifetimeKind::Two) => {
663+
Self::ExpectedSignatureTwo {
664+
leading_ellipsis,
665+
ty_or_sig,
666+
trait_path,
667+
lifetime_1,
668+
lifetime_2,
669+
}
670+
}
671+
(ActualImplExpectedKind::Signature, ActualImplExpectedLifetimeKind::Any) => {
672+
Self::ExpectedSignatureAny { leading_ellipsis, ty_or_sig, trait_path, lifetime_1 }
673+
}
674+
(ActualImplExpectedKind::Signature, ActualImplExpectedLifetimeKind::Some) => {
675+
Self::ExpectedSignatureSome { leading_ellipsis, ty_or_sig, trait_path, lifetime_1 }
676+
}
677+
(ActualImplExpectedKind::Signature, ActualImplExpectedLifetimeKind::Nothing) => {
678+
Self::ExpectedSignatureNothing { leading_ellipsis, ty_or_sig, trait_path }
679+
}
680+
(ActualImplExpectedKind::Passive, ActualImplExpectedLifetimeKind::Two) => {
681+
Self::ExpectedPassiveTwo {
682+
leading_ellipsis,
683+
ty_or_sig,
684+
trait_path,
685+
lifetime_1,
686+
lifetime_2,
687+
}
688+
}
689+
(ActualImplExpectedKind::Passive, ActualImplExpectedLifetimeKind::Any) => {
690+
Self::ExpectedPassiveAny { leading_ellipsis, ty_or_sig, trait_path, lifetime_1 }
691+
}
692+
(ActualImplExpectedKind::Passive, ActualImplExpectedLifetimeKind::Some) => {
693+
Self::ExpectedPassiveSome { leading_ellipsis, ty_or_sig, trait_path, lifetime_1 }
694+
}
695+
(ActualImplExpectedKind::Passive, ActualImplExpectedLifetimeKind::Nothing) => {
696+
Self::ExpectedPassiveNothing { leading_ellipsis, ty_or_sig, trait_path }
697+
}
698+
(ActualImplExpectedKind::Other, ActualImplExpectedLifetimeKind::Two) => {
699+
Self::ExpectedOtherTwo {
700+
leading_ellipsis,
701+
ty_or_sig,
702+
trait_path,
703+
lifetime_1,
704+
lifetime_2,
705+
}
706+
}
707+
(ActualImplExpectedKind::Other, ActualImplExpectedLifetimeKind::Any) => {
708+
Self::ExpectedOtherAny { leading_ellipsis, ty_or_sig, trait_path, lifetime_1 }
709+
}
710+
(ActualImplExpectedKind::Other, ActualImplExpectedLifetimeKind::Some) => {
711+
Self::ExpectedOtherSome { leading_ellipsis, ty_or_sig, trait_path, lifetime_1 }
712+
}
713+
(ActualImplExpectedKind::Other, ActualImplExpectedLifetimeKind::Nothing) => {
714+
Self::ExpectedOtherNothing { leading_ellipsis, ty_or_sig, trait_path }
715+
}
716+
}
717+
}
718+
}
719+
568720
#[derive(Diagnostic)]
569721
#[diag(infer::trait_placeholder_mismatch)]
570722
pub struct TraitPlaceholderMismatch {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::errors::{ActualImplExplNotes, TraitPlaceholderMismatch};
1+
use crate::errors::{
2+
ActualImplExpectedKind, ActualImplExpectedLifetimeKind, ActualImplExplNotes,
3+
TraitPlaceholderMismatch,
4+
};
25
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
36
use crate::infer::lexical_region_resolve::RegionResolutionError;
47
use crate::infer::ValuePairs;
@@ -414,52 +417,54 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
414417
}
415418
});
416419
(
417-
"signature",
420+
ActualImplExpectedKind::Signature,
418421
closure_sig.to_string(),
419422
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
420423
)
421424
} else {
422425
(
423-
"other",
426+
ActualImplExpectedKind::Other,
424427
self_ty.to_string(),
425428
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
426429
)
427430
}
428431
} else if passive_voice {
429432
(
430-
"passive",
433+
ActualImplExpectedKind::Passive,
431434
expected_trait_ref.map(|tr| tr.self_ty()).to_string(),
432435
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
433436
)
434437
} else {
435438
(
436-
"other",
439+
ActualImplExpectedKind::Other,
437440
expected_trait_ref.map(|tr| tr.self_ty()).to_string(),
438441
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
439442
)
440443
};
441444

442445
let (lt_kind, lifetime_1, lifetime_2) = match (has_sub, has_sup) {
443-
(Some(n1), Some(n2)) => ("two", std::cmp::min(n1, n2), std::cmp::max(n1, n2)),
444-
(Some(n), _) | (_, Some(n)) => ("any", n, 0),
446+
(Some(n1), Some(n2)) => {
447+
(ActualImplExpectedLifetimeKind::Two, std::cmp::min(n1, n2), std::cmp::max(n1, n2))
448+
}
449+
(Some(n), _) | (_, Some(n)) => (ActualImplExpectedLifetimeKind::Any, n, 0),
445450
(None, None) => {
446451
if let Some(n) = expected_has_vid {
447-
("some", n, 0)
452+
(ActualImplExpectedLifetimeKind::Some, n, 0)
448453
} else {
449-
("nothing", 0, 0)
454+
(ActualImplExpectedLifetimeKind::Nothing, 0, 0)
450455
}
451456
}
452457
};
453458

454-
let note_1 = ActualImplExplNotes::Expected {
455-
leading_ellipsis,
459+
let note_1 = ActualImplExplNotes::new_expected(
456460
kind,
461+
lt_kind,
462+
leading_ellipsis,
457463
ty_or_sig,
458464
trait_path,
459-
lt_kind,
460465
lifetime_1,
461466
lifetime_2,
462-
};
467+
);
463468

464469
let mut actual_trait_ref = highlight_trait_ref(actual_trait_ref);
465470
actual_trait_ref.highlight.maybe_highlighting_region(vid, actual_has_vid);
@@ -471,19 +476,26 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
471476

472477
let trait_path_2 = actual_trait_ref.map(|tr| tr.print_only_trait_path()).to_string();
473478
let ty = actual_trait_ref.map(|tr| tr.self_ty()).to_string();
474-
let kind_2 = if same_self_type {
475-
"implements_trait"
476-
} else if passive_voice {
477-
"implemented_for_ty"
478-
} else {
479-
"ty_implements"
480-
};
481-
482479
let has_lifetime = actual_has_vid.is_some();
483480
let lifetime = actual_has_vid.unwrap_or_default();
484481

485-
let note_2 =
486-
ActualImplExplNotes::ButActually { kind_2, trait_path_2, ty, has_lifetime, lifetime };
482+
let note_2 = if same_self_type {
483+
ActualImplExplNotes::ButActuallyImplementsTrait { trait_path_2, has_lifetime, lifetime }
484+
} else if passive_voice {
485+
ActualImplExplNotes::ButActuallyImplementedForTy {
486+
trait_path_2,
487+
ty,
488+
has_lifetime,
489+
lifetime,
490+
}
491+
} else {
492+
ActualImplExplNotes::ButActuallyTyImplements {
493+
trait_path_2,
494+
ty,
495+
has_lifetime,
496+
lifetime,
497+
}
498+
};
487499

488500
vec![note_1, note_2]
489501
}

0 commit comments

Comments
 (0)