Skip to content

Commit 39c5d6d

Browse files
committed
Factor out InherentImplCannotUnsafe
1 parent 6355cd3 commit 39c5d6d

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list}
2626
use rustc_ast::*;
2727
use rustc_ast_pretty::pprust::{self, State};
2828
use rustc_data_structures::fx::FxIndexMap;
29-
use rustc_errors::DiagCtxtHandle;
29+
use rustc_errors::{DiagCtxtHandle, E0197};
3030
use rustc_feature::Features;
3131
use rustc_parse::validate_attr;
3232
use rustc_session::Session;
@@ -1016,12 +1016,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10161016
errors::VisibilityNotPermittedNote::IndividualImplItems,
10171017
);
10181018
if let &Safety::Unsafe(span) = safety {
1019-
self.dcx().emit_err(errors::InherentImplCannotUnsafe {
1020-
span: self_ty.span,
1021-
annotation_span: span,
1022-
annotation: "unsafe",
1023-
self_ty: self_ty.span,
1024-
});
1019+
self.dcx()
1020+
.create_err(errors::InherentImplCannot {
1021+
span: self_ty.span,
1022+
annotation_span: span,
1023+
annotation: "unsafe",
1024+
self_ty: self_ty.span,
1025+
only_trait: true,
1026+
})
1027+
.with_code(E0197)
1028+
.emit();
10251029
}
10261030
if let &ImplPolarity::Negative(span) = polarity {
10271031
self.dcx().emit_err(error(span, "negative", false));

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -478,18 +478,6 @@ pub(crate) struct InherentImplCannot<'a> {
478478
pub only_trait: bool,
479479
}
480480

481-
#[derive(Diagnostic)]
482-
#[diag(ast_passes_inherent_cannot_be, code = E0197)]
483-
pub(crate) struct InherentImplCannotUnsafe<'a> {
484-
#[primary_span]
485-
pub span: Span,
486-
#[label(ast_passes_because)]
487-
pub annotation_span: Span,
488-
pub annotation: &'a str,
489-
#[label(ast_passes_type)]
490-
pub self_ty: Span,
491-
}
492-
493481
#[derive(Diagnostic)]
494482
#[diag(ast_passes_unsafe_item)]
495483
pub(crate) struct UnsafeItem {

tests/ui/error-codes/E0197.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | unsafe impl Foo { }
55
| ------ ^^^ inherent impl for this type
66
| |
77
| unsafe because of this
8+
|
9+
= note: only trait implementations may be annotated with unsafe
810

911
error: aborting due to 1 previous error
1012

tests/ui/traits/safety-inherent-impl.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | unsafe impl SomeStruct {
55
| ------ ^^^^^^^^^^ inherent impl for this type
66
| |
77
| unsafe because of this
8+
|
9+
= note: only trait implementations may be annotated with unsafe
810

911
error: aborting due to 1 previous error
1012

0 commit comments

Comments
 (0)