Skip to content

Commit 945b476

Browse files
committed
each left 1
1 parent fcd730c commit 945b476

File tree

5 files changed

+131
-66
lines changed

5 files changed

+131
-66
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// #![deny(rustc::untranslatable_diagnostic)]
2+
// #![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use std::collections::VecDeque;
25
use std::rc::Rc;
36

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// #![deny(rustc::untranslatable_diagnostic)]
2+
// #![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use rustc_data_structures::fx::FxHashMap;
25
use rustc_data_structures::vec_map::VecMap;
36
use rustc_hir::def_id::LocalDefId;
@@ -16,7 +19,9 @@ use rustc_span::Span;
1619
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
1720
use rustc_trait_selection::traits::TraitEngineExt as _;
1821

19-
use crate::session_diagnostics::ConstNotUsedTraitAlias;
22+
use crate::session_diagnostics::{
23+
ConstNotUsedTraitAlias, OpaqueTyDefineErrCause, OpaqueTypeNotDefine, UnusedTypeParameter,
24+
};
2025

2126
use super::RegionInferenceContext;
2227

@@ -374,15 +379,12 @@ fn check_opaque_type_parameter_valid(
374379
let arg_is_param = match arg.unpack() {
375380
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
376381
GenericArgKind::Lifetime(lt) if lt.is_static() => {
377-
tcx.sess
378-
.struct_span_err(span, "non-defining opaque type use in defining scope")
379-
.span_label(
380-
tcx.def_span(opaque_generics.param_at(i, tcx).def_id),
381-
"cannot use static lifetime; use a bound lifetime \
382-
instead or remove the lifetime parameter from the \
383-
opaque type",
384-
)
385-
.emit();
382+
tcx.sess.emit_err(OpaqueTypeNotDefine {
383+
cause: OpaqueTyDefineErrCause::UsedStaticLifetime {
384+
span: tcx.def_span(opaque_generics.param_at(i, tcx).def_id),
385+
},
386+
span,
387+
});
386388
return false;
387389
}
388390
GenericArgKind::Lifetime(lt) => {
@@ -396,17 +398,14 @@ fn check_opaque_type_parameter_valid(
396398
} else {
397399
// Prevent `fn foo() -> Foo<u32>` from being defining.
398400
let opaque_param = opaque_generics.param_at(i, tcx);
399-
tcx.sess
400-
.struct_span_err(span, "non-defining opaque type use in defining scope")
401-
.span_note(
402-
tcx.def_span(opaque_param.def_id),
403-
&format!(
404-
"used non-generic {} `{}` for generic parameter",
405-
opaque_param.kind.descr(),
406-
arg,
407-
),
408-
)
409-
.emit();
401+
tcx.sess.emit_err(OpaqueTypeNotDefine {
402+
cause: OpaqueTyDefineErrCause::NonGenericUsed {
403+
span: tcx.def_span(opaque_param.def_id),
404+
descr: opaque_param.kind.descr().to_string(),
405+
arg: arg.to_string(),
406+
},
407+
span,
408+
});
410409
return false;
411410
}
412411
}
@@ -418,6 +417,7 @@ fn check_opaque_type_parameter_valid(
418417
.into_iter()
419418
.map(|i| tcx.def_span(opaque_generics.param_at(i, tcx).def_id))
420419
.collect();
420+
//FIXME: wait for lists?
421421
tcx.sess
422422
.struct_span_err(span, "non-defining opaque type use in defining scope")
423423
.span_note(spans, &format!("{} used multiple times", descr))
@@ -525,18 +525,13 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
525525
self.tcx.lifetimes.re_root_empty
526526
}
527527
None => {
528-
self.tcx
529-
.sess
530-
.struct_span_err(self.span, "non-defining opaque type use in defining scope")
531-
.span_label(
532-
self.span,
533-
format!(
534-
"lifetime `{}` is part of concrete type but not used in \
535-
parameter list of the `impl Trait` type alias",
536-
r
537-
),
538-
)
539-
.emit();
528+
self.tcx.sess.emit_err(OpaqueTypeNotDefine {
529+
cause: OpaqueTyDefineErrCause::UnusedLifetime {
530+
span: self.span,
531+
r: r.to_string(),
532+
},
533+
span: self.span,
534+
});
540535

541536
self.tcx().lifetimes.re_static
542537
}
@@ -608,17 +603,7 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
608603
Some(u) => panic!("type mapped to unexpected kind: {:?}", u),
609604
None => {
610605
debug!(?param, ?self.map);
611-
self.tcx
612-
.sess
613-
.struct_span_err(
614-
self.span,
615-
&format!(
616-
"type parameter `{}` is part of concrete type but not \
617-
used in parameter list for the `impl Trait` type alias",
618-
ty
619-
),
620-
)
621-
.emit();
606+
self.tcx.sess.emit_err(UnusedTypeParameter { ty, span: self.span });
622607

623608
self.tcx().ty_error()
624609
}

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,56 @@ pub(crate) enum RegionNameLabels<'a> {
536536
location: &'a str,
537537
},
538538
}
539+
540+
#[derive(SessionDiagnostic)]
541+
#[diag(borrowck::type_parameter_not_used_in_trait_type_alias)]
542+
pub(crate) struct UnusedTypeParameter<'tcx> {
543+
pub ty: Ty<'tcx>,
544+
#[primary_span]
545+
pub span: Span,
546+
}
547+
548+
#[derive(SessionDiagnostic)]
549+
#[diag(borrowck::non_defining_opaque_type)]
550+
pub(crate) struct OpaqueTypeNotDefine {
551+
#[subdiagnostic]
552+
pub cause: OpaqueTyDefineErrCause,
553+
#[primary_span]
554+
pub span: Span,
555+
}
556+
557+
#[derive(SessionSubdiagnostic)]
558+
pub(crate) enum OpaqueTyDefineErrCause {
559+
#[label(borrowck::lifetime_not_used_in_trait_type_alias)]
560+
UnusedLifetime {
561+
#[primary_span]
562+
span: Span,
563+
r: String,
564+
},
565+
#[note(borrowck::used_non_generic_for_generic)]
566+
NonGenericUsed {
567+
#[primary_span]
568+
span: Span,
569+
descr: String,
570+
arg: String,
571+
},
572+
#[label(borrowck::cannot_use_static_lifetime_here)]
573+
UsedStaticLifetime {
574+
#[primary_span]
575+
span: Span,
576+
},
577+
}
578+
579+
#[derive(SessionSubdiagnostic)]
580+
pub(crate) enum DefiningTypeNote<'a> {
581+
#[note(borrowck::define_type_with_closure_substs)]
582+
Closure { type_name: &'a str, subsets: &'a str },
583+
#[note(borrowck::define_type_with_generator_substs)]
584+
Generator { type_name: &'a str, subsets: &'a str },
585+
#[note(borrowck::define_type)]
586+
FnDef { type_name: &'a str },
587+
#[note(borrowck::define_const_type)]
588+
Const { type_name: &'a str },
589+
#[note(borrowck::define_inline_constant_type)]
590+
InlineConst { type_name: &'a str },
591+
}

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// #![deny(rustc::untranslatable_diagnostic)]
2+
// #![deny(rustc::diagnostic_outside_of_impl)]
3+
14
//! Code to extract the universally quantified regions declared on a
25
//! function and the relationships between them. For example:
36
//!
@@ -27,6 +30,7 @@ use rustc_middle::ty::{self, InlineConstSubsts, InlineConstSubstsParts, RegionVi
2730
use std::iter;
2831

2932
use crate::nll::ToRegionVid;
33+
use crate::session_diagnostics::DefiningTypeNote;
3034

3135
#[derive(Debug)]
3236
pub struct UniversalRegions<'tcx> {
@@ -339,11 +343,9 @@ impl<'tcx> UniversalRegions<'tcx> {
339343
pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diagnostic) {
340344
match self.defining_ty {
341345
DefiningTy::Closure(def_id, substs) => {
342-
err.note(&format!(
343-
"defining type: {} with closure substs {:#?}",
344-
tcx.def_path_str_with_substs(def_id, substs),
345-
&substs[tcx.generics_of(def_id).parent_count..],
346-
));
346+
let type_name = &tcx.def_path_str_with_substs(def_id, substs);
347+
let subsets = &format!("{:#?}", &substs[tcx.generics_of(def_id).parent_count..]);
348+
err.subdiagnostic(DefiningTypeNote::Closure { type_name, subsets });
347349

348350
// FIXME: It'd be nice to print the late-bound regions
349351
// here, but unfortunately these wind up stored into
@@ -356,11 +358,9 @@ impl<'tcx> UniversalRegions<'tcx> {
356358
});
357359
}
358360
DefiningTy::Generator(def_id, substs, _) => {
359-
err.note(&format!(
360-
"defining type: {} with generator substs {:#?}",
361-
tcx.def_path_str_with_substs(def_id, substs),
362-
&substs[tcx.generics_of(def_id).parent_count..],
363-
));
361+
let type_name = &tcx.def_path_str_with_substs(def_id, substs);
362+
let subsets = &format!("{:#?}", &substs[tcx.generics_of(def_id).parent_count..]);
363+
err.subdiagnostic(DefiningTypeNote::Generator { type_name, subsets });
364364

365365
// FIXME: As above, we'd like to print out the region
366366
// `r` but doing so is not stable across architectures
@@ -371,22 +371,16 @@ impl<'tcx> UniversalRegions<'tcx> {
371371
});
372372
}
373373
DefiningTy::FnDef(def_id, substs) => {
374-
err.note(&format!(
375-
"defining type: {}",
376-
tcx.def_path_str_with_substs(def_id, substs),
377-
));
374+
let type_name = &tcx.def_path_str_with_substs(def_id, substs);
375+
err.subdiagnostic(DefiningTypeNote::FnDef { type_name });
378376
}
379377
DefiningTy::Const(def_id, substs) => {
380-
err.note(&format!(
381-
"defining constant type: {}",
382-
tcx.def_path_str_with_substs(def_id, substs),
383-
));
378+
let type_name = &tcx.def_path_str_with_substs(def_id, substs);
379+
err.subdiagnostic(DefiningTypeNote::Const { type_name });
384380
}
385381
DefiningTy::InlineConst(def_id, substs) => {
386-
err.note(&format!(
387-
"defining inline constant type: {}",
388-
tcx.def_path_str_with_substs(def_id, substs),
389-
));
382+
let type_name = &tcx.def_path_str_with_substs(def_id, substs);
383+
err.subdiagnostic(DefiningTypeNote::InlineConst { type_name });
390384
}
391385
}
392386
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,33 @@ borrowck_yield_type_is_type =
218218
219219
borrowck_lifetime_appears_here_in_impl =
220220
lifetime `{$rg_name}` appears in the `impl`'s {$location}
221+
222+
borrowck_type_parameter_not_used_in_trait_type_alias =
223+
type parameter `{$ty}` is part of concrete type but not used in parameter list for the `impl Trait` type alias
224+
225+
borrowck_non_defining_opaque_type =
226+
non-defining opaque type use in defining scope
227+
228+
borrowck_lifetime_not_used_in_trait_type_alias =
229+
lifetime `{$r}` is part of concrete type but not used in parameter list of the `impl Trait` type alias
230+
231+
borrowck_used_non_generic_for_generic =
232+
used non-generic {$descr} `{$arg}` for generic parameter
233+
234+
borrowck_cannot_use_static_lifetime_here =
235+
cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
236+
237+
borrowck_define_inline_constant_type =
238+
defining inline constant type: {$type_name}
239+
240+
borrowck_define_const_type =
241+
defining constant type: {$type_name}
242+
243+
borrowck_define_type =
244+
defining type: {$type_name}
245+
246+
borrowck_define_type_with_generator_substs =
247+
defining type: {$type_name} with generator substs {$subsets}
248+
249+
borrowck_define_type_with_closure_substs =
250+
defining type: {$type_name} with closure substs {$subsets}

0 commit comments

Comments
 (0)