Skip to content

Commit e7f9f48

Browse files
committed
Auto merge of #115853 - obeis:hir-analysis-migrate-diagnostics-6, r=compiler-errors
Migrate `rustc_hir_analysis` to session diagnostic [Part 6] Part 6: Finish `coherence/inherent_impls.rs` file
2 parents 56e1aaa + 8723af5 commit e7f9f48

File tree

3 files changed

+116
-77
lines changed

3 files changed

+116
-77
lines changed

compiler/rustc_hir_analysis/messages.ftl

+28
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,34 @@ hir_analysis_impl_not_marked_default = `{$ident}` specializes an item from a par
9595
hir_analysis_impl_not_marked_default_err = `{$ident}` specializes an item from a parent `impl`, but that item is not marked `default`
9696
.note = parent implementation is in crate `{$cname}`
9797
98+
hir_analysis_inherent_dyn = cannot define inherent `impl` for a dyn auto trait
99+
.label = impl requires at least one non-auto trait
100+
.note = define and implement a new trait or type instead
101+
102+
hir_analysis_inherent_nominal = no nominal type found for inherent implementation
103+
.label = impl requires a nominal type
104+
.note = either implement a trait on it or create a newtype to wrap it instead
105+
hir_analysis_inherent_primitive_ty = cannot define inherent `impl` for primitive types
106+
.help = consider using an extension trait instead
107+
108+
hir_analysis_inherent_primitive_ty_note = you could also try moving the reference to uses of `{$subty}` (such as `self`) within the implementation
109+
110+
hir_analysis_inherent_ty_outside = cannot define inherent `impl` for a type outside of the crate where the type is defined
111+
.help = consider moving this inherent impl into the crate defining the type if possible
112+
.span_help = alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
113+
114+
hir_analysis_inherent_ty_outside_new = cannot define inherent `impl` for a type outside of the crate where the type is defined
115+
.label = impl for type defined outside of crate.
116+
.note = define and implement a trait or new type instead
117+
118+
hir_analysis_inherent_ty_outside_primitive = cannot define inherent `impl` for primitive types outside of `core`
119+
.help = consider moving this inherent impl into `core` if possible
120+
.span_help = alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
121+
122+
hir_analysis_inherent_ty_outside_relevant = cannot define inherent `impl` for a type outside of the crate where the type is defined
123+
.help = consider moving this inherent impl into the crate defining the type if possible
124+
.span_help = alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
125+
98126
hir_analysis_invalid_union_field =
99127
field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
100128
.note = union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+16-77
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
//! `tcx.inherent_impls(def_id)`). That value, however,
88
//! is computed by selecting an idea from this table.
99
10-
use rustc_errors::struct_span_err;
1110
use rustc_hir as hir;
1211
use rustc_hir::def::DefKind;
1312
use rustc_hir::def_id::{DefId, LocalDefId};
1413
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
1514
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
1615
use rustc_span::symbol::sym;
1716

17+
use crate::errors;
18+
1819
/// On-demand query: yields a map containing all types mapped to their inherent impls.
1920
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
2021
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
@@ -45,14 +46,6 @@ struct InherentCollect<'tcx> {
4546
impls_map: CrateInherentImpls,
4647
}
4748

48-
const INTO_CORE: &str = "consider moving this inherent impl into `core` if possible";
49-
const INTO_DEFINING_CRATE: &str =
50-
"consider moving this inherent impl into the crate defining the type if possible";
51-
const ADD_ATTR_TO_TY: &str = "alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type \
52-
and `#[rustc_allow_incoherent_impl]` to the relevant impl items";
53-
const ADD_ATTR: &str =
54-
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
55-
5649
impl<'tcx> InherentCollect<'tcx> {
5750
fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id: DefId) {
5851
if let Some(ty_def_id) = ty_def_id.as_local() {
@@ -69,30 +62,17 @@ impl<'tcx> InherentCollect<'tcx> {
6962

7063
if !self.tcx.has_attr(ty_def_id, sym::rustc_has_incoherent_inherent_impls) {
7164
let impl_span = self.tcx.def_span(impl_def_id);
72-
struct_span_err!(
73-
self.tcx.sess,
74-
impl_span,
75-
E0390,
76-
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
77-
)
78-
.help(INTO_DEFINING_CRATE)
79-
.span_help(impl_span, ADD_ATTR_TO_TY)
80-
.emit();
65+
self.tcx.sess.emit_err(errors::InherentTyOutside { span: impl_span });
8166
return;
8267
}
8368

8469
for &impl_item in items {
8570
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
8671
let impl_span = self.tcx.def_span(impl_def_id);
87-
struct_span_err!(
88-
self.tcx.sess,
89-
impl_span,
90-
E0390,
91-
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
92-
)
93-
.help(INTO_DEFINING_CRATE)
94-
.span_help(self.tcx.def_span(impl_item), ADD_ATTR)
95-
.emit();
72+
self.tcx.sess.emit_err(errors::InherentTyOutsideRelevant {
73+
span: impl_span,
74+
help_span: self.tcx.def_span(impl_item),
75+
});
9676
return;
9777
}
9878
}
@@ -104,16 +84,7 @@ impl<'tcx> InherentCollect<'tcx> {
10484
}
10585
} else {
10686
let impl_span = self.tcx.def_span(impl_def_id);
107-
struct_span_err!(
108-
self.tcx.sess,
109-
impl_span,
110-
E0116,
111-
"cannot define inherent `impl` for a type outside of the crate \
112-
where the type is defined"
113-
)
114-
.span_label(impl_span, "impl for type defined outside of crate.")
115-
.note("define and implement a trait or new type instead")
116-
.emit();
87+
self.tcx.sess.emit_err(errors::InherentTyOutsideNew { span: impl_span });
11788
}
11889
}
11990

@@ -124,34 +95,20 @@ impl<'tcx> InherentCollect<'tcx> {
12495
for &impl_item in items {
12596
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
12697
let span = self.tcx.def_span(impl_def_id);
127-
struct_span_err!(
128-
self.tcx.sess,
98+
self.tcx.sess.emit_err(errors::InherentTyOutsidePrimitive {
12999
span,
130-
E0390,
131-
"cannot define inherent `impl` for primitive types outside of `core`",
132-
)
133-
.help(INTO_CORE)
134-
.span_help(self.tcx.def_span(impl_item), ADD_ATTR)
135-
.emit();
100+
help_span: self.tcx.def_span(impl_item),
101+
});
136102
return;
137103
}
138104
}
139105
} else {
140106
let span = self.tcx.def_span(impl_def_id);
141-
let mut err = struct_span_err!(
142-
self.tcx.sess,
143-
span,
144-
E0390,
145-
"cannot define inherent `impl` for primitive types",
146-
);
147-
err.help("consider using an extension trait instead");
107+
let mut note = None;
148108
if let ty::Ref(_, subty, _) = ty.kind() {
149-
err.note(format!(
150-
"you could also try moving the reference to \
151-
uses of `{subty}` (such as `self`) within the implementation"
152-
));
109+
note = Some(errors::InherentPrimitiveTyNote { subty: *subty });
153110
}
154-
err.emit();
111+
self.tcx.sess.emit_err(errors::InherentPrimitiveTy { span, note });
155112
return;
156113
}
157114
}
@@ -178,15 +135,7 @@ impl<'tcx> InherentCollect<'tcx> {
178135
self.check_def_id(id, self_ty, data.principal_def_id().unwrap());
179136
}
180137
ty::Dynamic(..) => {
181-
struct_span_err!(
182-
self.tcx.sess,
183-
item_span,
184-
E0785,
185-
"cannot define inherent `impl` for a dyn auto trait"
186-
)
187-
.span_label(item_span, "impl requires at least one non-auto trait")
188-
.note("define and implement a new trait or type instead")
189-
.emit();
138+
self.tcx.sess.emit_err(errors::InherentDyn { span: item_span });
190139
}
191140
ty::Bool
192141
| ty::Char
@@ -202,17 +151,7 @@ impl<'tcx> InherentCollect<'tcx> {
202151
| ty::FnPtr(_)
203152
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
204153
ty::Alias(..) | ty::Param(_) => {
205-
let mut err = struct_span_err!(
206-
self.tcx.sess,
207-
item_span,
208-
E0118,
209-
"no nominal type found for inherent implementation"
210-
);
211-
212-
err.span_label(item_span, "impl requires a nominal type")
213-
.note("either implement a trait on it or create a newtype to wrap it instead");
214-
215-
err.emit();
154+
self.tcx.sess.emit_err(errors::InherentNominal { span: item_span });
216155
}
217156
ty::FnDef(..)
218157
| ty::Closure(..)

compiler/rustc_hir_analysis/src/errors.rs

+72
Original file line numberDiff line numberDiff line change
@@ -943,3 +943,75 @@ pub struct AssocBoundOnConst {
943943
pub span: Span,
944944
pub descr: &'static str,
945945
}
946+
947+
#[derive(Diagnostic)]
948+
#[diag(hir_analysis_inherent_ty_outside, code = "E0390")]
949+
#[help]
950+
pub struct InherentTyOutside {
951+
#[primary_span]
952+
#[help(hir_analysis_span_help)]
953+
pub span: Span,
954+
}
955+
956+
#[derive(Diagnostic)]
957+
#[diag(hir_analysis_inherent_ty_outside_relevant, code = "E0390")]
958+
#[help]
959+
pub struct InherentTyOutsideRelevant {
960+
#[primary_span]
961+
pub span: Span,
962+
#[help(hir_analysis_span_help)]
963+
pub help_span: Span,
964+
}
965+
966+
#[derive(Diagnostic)]
967+
#[diag(hir_analysis_inherent_ty_outside_new, code = "E0116")]
968+
#[note]
969+
pub struct InherentTyOutsideNew {
970+
#[primary_span]
971+
#[label]
972+
pub span: Span,
973+
}
974+
975+
#[derive(Diagnostic)]
976+
#[diag(hir_analysis_inherent_ty_outside_primitive, code = "E0390")]
977+
#[help]
978+
pub struct InherentTyOutsidePrimitive {
979+
#[primary_span]
980+
pub span: Span,
981+
#[help(hir_analysis_span_help)]
982+
pub help_span: Span,
983+
}
984+
985+
#[derive(Diagnostic)]
986+
#[diag(hir_analysis_inherent_primitive_ty, code = "E0390")]
987+
#[help]
988+
pub struct InherentPrimitiveTy<'a> {
989+
#[primary_span]
990+
pub span: Span,
991+
#[subdiagnostic]
992+
pub note: Option<InherentPrimitiveTyNote<'a>>,
993+
}
994+
995+
#[derive(Subdiagnostic)]
996+
#[note(hir_analysis_inherent_primitive_ty_note)]
997+
pub struct InherentPrimitiveTyNote<'a> {
998+
pub subty: Ty<'a>,
999+
}
1000+
1001+
#[derive(Diagnostic)]
1002+
#[diag(hir_analysis_inherent_dyn, code = "E0785")]
1003+
#[note]
1004+
pub struct InherentDyn {
1005+
#[primary_span]
1006+
#[label]
1007+
pub span: Span,
1008+
}
1009+
1010+
#[derive(Diagnostic)]
1011+
#[diag(hir_analysis_inherent_nominal, code = "E0118")]
1012+
#[note]
1013+
pub struct InherentNominal {
1014+
#[primary_span]
1015+
#[label]
1016+
pub span: Span,
1017+
}

0 commit comments

Comments
 (0)