Skip to content

Commit 0634b01

Browse files
committed
Partial work on static_impl_trait.rs
1 parent 3935a81 commit 0634b01

File tree

3 files changed

+221
-111
lines changed

3 files changed

+221
-111
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,55 @@ infer_trait_impl_diff = `impl` item signature doesn't match `trait` item signatu
221221
infer_tid_rel_help = verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
222222
infer_tid_consider_borriwing = consider borrowing this type parameter in the trait
223223
infer_tid_param_help = the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
224+
225+
infer_dtcs_has_lifetime_req_label = this has an implicit `'static` lifetime requirement
226+
infer_dtcs_introduces_requirement = calling this method introduces the `impl`'s 'static` requirement
227+
infer_dtcs_has_req_note = the used `impl` has a `'static` requirement
228+
infer_dtcs_suggestion = consider relaxing the implicit `'static` requirement
229+
230+
infer_but_calling_introduces = {$has_param_name ->
231+
[true] `{$param_name}`
232+
*[false] `fn` parameter
233+
} has {$lifetime_kind ->
234+
[named] lifetime `{lifetime}`
235+
*[anon] an anonymous lifetime `'_`
236+
} but calling `{assoc_item}` introduces an implicit `'static` lifetime requirement
237+
.label1 = {$has_lifetime ->
238+
[named] lifetime `{lifetime}`
239+
*[anon] an anonymous lifetime `'_`
240+
}
241+
.label2 = ...is used and required to live as long as `'static` here because of an implicit lifetime bound on the {$has_impl_path ->
242+
[named] `impl` of `{$impl_path}`
243+
*[anon] inherent `impl`
244+
}
245+
246+
infer_but_needs_to_satisfy = {$has_param_name ->
247+
[true] `{$param_name}`
248+
*[false] `fn` parameter
249+
} has {$has_lifetime ->
250+
[named] lifetime `{lifetime}`
251+
*[anon] an anonymous lifetime `'_`
252+
} but it needs to satisfy a `'static` lifetime requirement
253+
.influencer = this data with {$has_lifetime ->
254+
[named] lifetime `{lifetime}`
255+
*[anon] an anonymous lifetime `'_`
256+
}...
257+
.require = {$spans_empty ->
258+
*[true] ...is used and required to live as long as `'static` here
259+
[false] ...and is required to live as long as `'static` here
260+
}
261+
.used_here = ...is used here...
262+
.introduced_by_bound = 'static` lifetime requirement introduced by this bound
263+
264+
infer_more_targeted = {$has_param_name ->
265+
[true] `{$param_name}`
266+
*[false] `fn` parameter
267+
} has {$has_lifetime ->
268+
[named] lifetime `{lifetime}`
269+
*[anon] an anonymous lifetime `'_`
270+
} but calling `{$ident}` introduces an implicit `'static` lifetime requirement
271+
272+
infer_ril_introduced_here = `'static` requirement introduced here
273+
infer_ril_introduced_by = requirement introduced by this return type
274+
infer_ril_because_of = because of this returned expression
275+
infer_ril_static_introduced_by = "`'static` lifetime requirement introduced by the return type

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{FnRetTy, Ty};
88
use rustc_macros::{Diagnostic, Subdiagnostic};
99
use rustc_middle::ty::{Region, TyCtxt};
1010
use rustc_span::symbol::kw;
11+
use rustc_span::Symbol;
1112
use rustc_span::{symbol::Ident, BytePos, Span};
1213

1314
use crate::infer::error_reporting::{
@@ -619,3 +620,99 @@ pub struct TraitImplDiff {
619620
pub expected: String,
620621
pub found: String,
621622
}
623+
624+
pub struct DynTraitConstraintSuggestion {
625+
pub span: Span,
626+
pub ident: Ident,
627+
}
628+
629+
impl AddSubdiagnostic for DynTraitConstraintSuggestion {
630+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
631+
let mut multi_span: MultiSpan = vec![self.span].into();
632+
multi_span.push_span_label(self.span, fluent::infer::dtcs_has_lifetime_req_label);
633+
multi_span.push_span_label(self.ident.span, fluent::infer::dtcs_introduces_requirement);
634+
diag.span_note(multi_span, fluent::infer::dtcs_has_req_note);
635+
diag.span_suggestion_verbose(
636+
self.span.shrink_to_hi(),
637+
fluent::infer::dtcs_suggestion,
638+
" + '_",
639+
Applicability::MaybeIncorrect,
640+
);
641+
}
642+
}
643+
644+
#[derive(SessionDiagnostic)]
645+
#[diag(infer::but_calling_introduces, code = "E0772")]
646+
pub struct ButCallingIntroduces {
647+
#[label(infer::label1)]
648+
pub param_ty_span: Span,
649+
#[primary_span]
650+
#[label(infer::label2)]
651+
pub cause_span: Span,
652+
653+
pub has_param_name: bool,
654+
pub param_name: String,
655+
pub has_lifetime: bool,
656+
pub lifetime: String,
657+
pub assoc_item: Symbol,
658+
pub has_impl_path: bool,
659+
pub impl_path: String,
660+
}
661+
662+
pub struct ReqIntroducedLocations {
663+
pub span: MultiSpan,
664+
pub spans: Vec<Span>,
665+
pub fn_decl_span: Span,
666+
pub cause_span: Span,
667+
pub add_label: bool,
668+
}
669+
670+
impl AddSubdiagnostic for ReqIntroducedLocations {
671+
fn add_to_diagnostic(mut self, diag: &mut rustc_errors::Diagnostic) {
672+
for sp in self.spans {
673+
self.span.push_span_label(sp, fluent::infer::ril_introduced_here);
674+
}
675+
676+
if self.add_label {
677+
self.span.push_span_label(self.fn_decl_span, fluent::infer::ril_introduced_by);
678+
}
679+
self.span.push_span_label(self.cause_span, fluent::infer::ril_because_of);
680+
diag.span_note(self.span, fluent::infer::ril_static_introduced_by);
681+
}
682+
}
683+
684+
pub struct MoreTargeted {
685+
pub ident: Symbol,
686+
}
687+
688+
impl AddSubdiagnostic for MoreTargeted {
689+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
690+
diag.code(rustc_errors::error_code!(E0772));
691+
diag.set_primary_message(fluent::infer::more_targeted);
692+
diag.set_arg("ident", self.ident);
693+
}
694+
}
695+
696+
#[derive(SessionDiagnostic)]
697+
#[diag(infer::but_needs_to_satisfy, code = "E0759")]
698+
pub struct ButNeedsToSatisfy {
699+
#[primary_span]
700+
pub sp: Span,
701+
#[label(infer::influencer)]
702+
pub influencer_point: Span,
703+
#[label(infer::used_here)]
704+
pub spans: Vec<Span>,
705+
#[label(infer::require)]
706+
pub require_span_as_label: Option<Span>,
707+
#[note(infer::require)]
708+
pub require_span_as_note: Option<Span>,
709+
#[note(infer::introduced_by_bound)]
710+
pub bound: Option<Span>,
711+
712+
#[subdiagnostic]
713+
pub req_introduces_loc: Option<ReqIntroducedLocations>,
714+
715+
pub spans_empty: bool,
716+
pub has_lifetime: bool,
717+
pub lifetime: String,
718+
}

0 commit comments

Comments
 (0)