Skip to content

Commit ef05f9e

Browse files
committed
Factor out InherentImplCannotUnsafe
1 parent 3048886 commit ef05f9e

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
@@ -27,7 +27,7 @@ use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list}
2727
use rustc_ast::*;
2828
use rustc_ast_pretty::pprust::{self, State};
2929
use rustc_data_structures::fx::FxIndexMap;
30-
use rustc_errors::DiagCtxtHandle;
30+
use rustc_errors::{DiagCtxtHandle, E0197};
3131
use rustc_feature::Features;
3232
use rustc_parse::validate_attr;
3333
use rustc_session::Session;
@@ -1013,12 +1013,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10131013
errors::VisibilityNotPermittedNote::IndividualImplItems,
10141014
);
10151015
if let &Safety::Unsafe(span) = safety {
1016-
self.dcx().emit_err(errors::InherentImplCannotUnsafe {
1017-
span: self_ty.span,
1018-
annotation_span: span,
1019-
annotation: "unsafe",
1020-
self_ty: self_ty.span,
1021-
});
1016+
self.dcx()
1017+
.create_err(errors::InherentImplCannot {
1018+
span: self_ty.span,
1019+
annotation_span: span,
1020+
annotation: "unsafe",
1021+
self_ty: self_ty.span,
1022+
only_trait: true,
1023+
})
1024+
.with_code(E0197)
1025+
.emit();
10221026
}
10231027
if let &ImplPolarity::Negative(span) = polarity {
10241028
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
@@ -477,18 +477,6 @@ pub(crate) struct InherentImplCannot<'a> {
477477
pub only_trait: bool,
478478
}
479479

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