Skip to content

Commit 2a1d748

Browse files
committed
Replace item names containing an error code with something more meaningful
or inline such functions if useless.
1 parent dec1d16 commit 2a1d748

19 files changed

+55
-50
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -632,20 +632,19 @@ impl<'a> AstValidator<'a> {
632632
}
633633
}
634634

635-
fn emit_e0568(&self, span: Span, ident: Span) {
636-
self.dcx().emit_err(errors::AutoTraitBounds { span, ident });
637-
}
638-
639635
fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
640636
if let [.., last] = &bounds[..] {
641637
let span = ident_span.shrink_to_hi().to(last.span());
642-
self.emit_e0568(span, ident_span);
638+
self.dcx().emit_err(errors::AutoTraitBounds { span, ident: ident_span });
643639
}
644640
}
645641

646642
fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
647643
if !where_clause.predicates.is_empty() {
648-
self.emit_e0568(where_clause.span, ident_span);
644+
// FIXME: The current diagnostic is misleading since it only talks about
645+
// super trait and lifetime bounds while we should just say “bounds”.
646+
self.dcx()
647+
.emit_err(errors::AutoTraitBounds { span: where_clause.span, ident: ident_span });
649648
}
650649
}
651650

compiler/rustc_hir_analysis/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ hir_analysis_inherent_ty_outside_relevant = cannot define inherent `impl` for a
201201
.help = consider moving this inherent impl into the crate defining the type if possible
202202
.span_help = alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
203203
204+
hir_analysis_invalid_receiver_ty = invalid `self` parameter type: `{$receiver_ty}`
205+
.note = type of `self` must be `Self` or a type that dereferences to it
206+
207+
hir_analysis_invalid_receiver_ty_help =
208+
consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
209+
204210
hir_analysis_invalid_union_field =
205211
field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
206212
.note = union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::autoderef::Autoderef;
22
use crate::collect::CollectItemTypesVisitor;
33
use crate::constrained_generic_params::{identify_constrained_generic_params, Parameter};
44
use crate::errors;
5+
use crate::fluent_generated as fluent;
56

67
use hir::intravisit::Visitor;
78
use rustc_ast as ast;
@@ -1636,10 +1637,6 @@ fn check_fn_or_method<'tcx>(
16361637
}
16371638
}
16381639

1639-
const HELP_FOR_SELF_TYPE: &str = "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
1640-
`self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
1641-
of the previous types except `Self`)";
1642-
16431640
#[instrument(level = "debug", skip(wfcx))]
16441641
fn check_method_receiver<'tcx>(
16451642
wfcx: &WfCheckingCtxt<'_, 'tcx>,
@@ -1675,7 +1672,7 @@ fn check_method_receiver<'tcx>(
16751672
if tcx.features().arbitrary_self_types {
16761673
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
16771674
// Report error; `arbitrary_self_types` was enabled.
1678-
return Err(e0307(tcx, span, receiver_ty));
1675+
return Err(tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty }));
16791676
}
16801677
} else {
16811678
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, false) {
@@ -1690,24 +1687,17 @@ fn check_method_receiver<'tcx>(
16901687
the `arbitrary_self_types` feature",
16911688
),
16921689
)
1693-
.with_help(HELP_FOR_SELF_TYPE)
1690+
.with_help(fluent::hir_analysis_invalid_receiver_ty_help)
16941691
.emit()
16951692
} else {
16961693
// Report error; would not have worked with `arbitrary_self_types`.
1697-
e0307(tcx, span, receiver_ty)
1694+
tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty })
16981695
});
16991696
}
17001697
}
17011698
Ok(())
17021699
}
17031700

1704-
fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) -> ErrorGuaranteed {
1705-
struct_span_code_err!(tcx.dcx(), span, E0307, "invalid `self` parameter type: {receiver_ty}")
1706-
.with_note("type of `self` must be `Self` or a type that dereferences to it")
1707-
.with_help(HELP_FOR_SELF_TYPE)
1708-
.emit()
1709-
}
1710-
17111701
/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
17121702
/// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
17131703
/// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more

compiler/rustc_hir_analysis/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1641,3 +1641,13 @@ pub struct NonConstRange {
16411641
#[primary_span]
16421642
pub span: Span,
16431643
}
1644+
1645+
#[derive(Diagnostic)]
1646+
#[diag(hir_analysis_invalid_receiver_ty, code = E0307)]
1647+
#[note]
1648+
#[help(hir_analysis_invalid_receiver_ty_help)]
1649+
pub struct InvalidReceiverTy<'tcx> {
1650+
#[primary_span]
1651+
pub span: Span,
1652+
pub receiver_ty: Ty<'tcx>,
1653+
}

compiler/rustc_hir_typeck/src/pat.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1228,16 +1228,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12281228
);
12291229
}
12301230
} else {
1231-
// Pattern has wrong number of fields.
1232-
let e =
1233-
self.e0023(pat.span, res, qpath, subpats, &variant.fields.raw, expected, had_err);
1231+
let e = self.emit_err_pat_wrong_number_of_fields(
1232+
pat.span,
1233+
res,
1234+
qpath,
1235+
subpats,
1236+
&variant.fields.raw,
1237+
expected,
1238+
had_err,
1239+
);
12341240
on_error(e);
12351241
return Ty::new_error(tcx, e);
12361242
}
12371243
pat_ty
12381244
}
12391245

1240-
fn e0023(
1246+
fn emit_err_pat_wrong_number_of_fields(
12411247
&self,
12421248
pat_span: Span,
12431249
res: Res,

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use rustc_arena::{DroplessArena, TypedArena};
1010
use rustc_ast::Mutability;
1111
use rustc_data_structures::fx::FxIndexSet;
1212
use rustc_data_structures::stack::ensure_sufficient_stack;
13-
use rustc_errors::{
14-
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan,
15-
};
13+
use rustc_errors::{codes::*, struct_span_code_err, Applicability, ErrorGuaranteed, MultiSpan};
1614
use rustc_hir::def::*;
1715
use rustc_hir::def_id::LocalDefId;
1816
use rustc_hir::{self as hir, BindingMode, ByRef, HirId};
@@ -24,7 +22,6 @@ use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
2422
use rustc_session::lint::builtin::{
2523
BINDINGS_WITH_VARIANT_NAME, IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS,
2624
};
27-
use rustc_session::Session;
2825
use rustc_span::hygiene::DesugaringKind;
2926
use rustc_span::{sym, Span};
3027

@@ -64,10 +61,6 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), Err
6461
visitor.error
6562
}
6663

67-
fn create_e0004(sess: &Session, sp: Span, error_message: String) -> Diag<'_> {
68-
struct_span_code_err!(sess.dcx(), sp, E0004, "{}", &error_message)
69-
}
70-
7164
#[derive(Debug, Copy, Clone, PartialEq)]
7265
enum RefutableFlag {
7366
Irrefutable,
@@ -975,10 +968,11 @@ fn report_non_exhaustive_match<'p, 'tcx>(
975968

976969
// FIXME: migration of this diagnostic will require list support
977970
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
978-
let mut err = create_e0004(
979-
cx.tcx.sess,
971+
let mut err = struct_span_code_err!(
972+
cx.tcx.dcx(),
980973
sp,
981-
format!("non-exhaustive patterns: {joined_patterns} not covered"),
974+
E0004,
975+
"non-exhaustive patterns: {joined_patterns} not covered"
982976
);
983977
err.span_label(
984978
sp,

tests/ui/async-await/inference_var_self_argument.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
trait Foo {
55
async fn foo(self: &dyn Foo) {
66
//~^ ERROR: `Foo` cannot be made into an object
7-
//~| ERROR invalid `self` parameter type: &dyn Foo
7+
//~| ERROR invalid `self` parameter type: `&dyn Foo`
88
todo!()
99
}
1010
}

tests/ui/async-await/inference_var_self_argument.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | async fn foo(self: &dyn Foo) {
1313
| ^^^ ...because method `foo` is `async`
1414
= help: consider moving `foo` to another trait
1515

16-
error[E0307]: invalid `self` parameter type: &dyn Foo
16+
error[E0307]: invalid `self` parameter type: `&dyn Foo`
1717
--> $DIR/inference_var_self_argument.rs:5:24
1818
|
1919
LL | async fn foo(self: &dyn Foo) {

tests/ui/async-await/issue-66312.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | if x.is_some() {
55
| ^^^^^^^^^^^ expected `bool`, found `()`
66

7-
error[E0307]: invalid `self` parameter type: T
7+
error[E0307]: invalid `self` parameter type: `T`
88
--> $DIR/issue-66312.rs:4:22
99
|
1010
LL | fn is_some(self: T);

tests/ui/feature-gates/feature-gate-dispatch-from-dyn-cell.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::cell::Cell;
44

55
trait Trait{
6-
fn cell(self: Cell<&Self>); //~ ERROR invalid `self` parameter type: Cell<&Self>
6+
fn cell(self: Cell<&Self>); //~ ERROR invalid `self` parameter type: `Cell<&Self>`
77
}
88

99
fn main() {}

tests/ui/feature-gates/feature-gate-dispatch-from-dyn-cell.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0307]: invalid `self` parameter type: Cell<&Self>
1+
error[E0307]: invalid `self` parameter type: `Cell<&Self>`
22
--> $DIR/feature-gate-dispatch-from-dyn-cell.rs:6:19
33
|
44
LL | fn cell(self: Cell<&Self>);

tests/ui/issues/issue-56806.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0307]: invalid `self` parameter type: Box<(dyn Trait + 'static)>
1+
error[E0307]: invalid `self` parameter type: `Box<(dyn Trait + 'static)>`
22
--> $DIR/issue-56806.rs:2:34
33
|
44
LL | fn dyn_instead_of_self(self: Box<dyn Trait>);

tests/ui/self/arbitrary-self-opaque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Bar = impl Sized;
66

77
impl Foo {
88
fn foo(self: Bar) {}
9-
//~^ ERROR: invalid `self` parameter type: Bar
9+
//~^ ERROR: invalid `self` parameter type: `Bar`
1010
}
1111

1212
fn main() {}

tests/ui/self/arbitrary-self-opaque.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | type Bar = impl Sized;
66
|
77
= note: `Bar` must be used in combination with a concrete type within the same module
88

9-
error[E0307]: invalid `self` parameter type: Bar
9+
error[E0307]: invalid `self` parameter type: `Bar`
1010
--> $DIR/arbitrary-self-opaque.rs:8:18
1111
|
1212
LL | fn foo(self: Bar) {}

tests/ui/span/issue-27522.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0307]: invalid `self` parameter type: &SomeType
1+
error[E0307]: invalid `self` parameter type: `&SomeType`
22
--> $DIR/issue-27522.rs:6:22
33
|
44
LL | fn handler(self: &SomeType);

tests/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ help: consider changing method `bar`'s `self` parameter to be `&self`
2626
LL | fn bar(self: &Self) {}
2727
| ~~~~~
2828

29-
error[E0307]: invalid `self` parameter type: ()
29+
error[E0307]: invalid `self` parameter type: `()`
3030
--> $DIR/object-unsafe-trait-should-use-where-sized.rs:6:18
3131
|
3232
LL | fn bar(self: ()) {}

tests/ui/traits/issue-78372.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ impl<T> DispatchFromDyn<Smaht<U, MISC>> for T {} //~ ERROR cannot find type `U`
66
//~| ERROR the trait `DispatchFromDyn` may only be implemented for a coercion between structures
77
trait Foo: X<u32> {}
88
trait X<T> {
9-
fn foo(self: Smaht<Self, T>); //~ ERROR: invalid `self`
9+
fn foo(self: Smaht<Self, T>); //~ ERROR: invalid `self` parameter type
1010
}
1111
trait Marker {}
1212
impl Marker for dyn Foo {}

tests/ui/traits/issue-78372.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ LL | trait X<T> {
7979
LL | fn foo(self: Smaht<Self, T>);
8080
| ^^^^^^^^^^^^^^ ...because method `foo`'s `self` parameter cannot be dispatched on
8181

82-
error[E0307]: invalid `self` parameter type: Smaht<Self, T>
82+
error[E0307]: invalid `self` parameter type: `Smaht<Self, T>`
8383
--> $DIR/issue-78372.rs:9:18
8484
|
8585
LL | fn foo(self: Smaht<Self, T>);

tests/ui/ufcs/ufcs-explicit-self-bad.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | fn dummy2(&self);
1515
= note: expected signature `fn(&&'a Bar<_>)`
1616
found signature `fn(&Bar<_>)`
1717

18-
error[E0307]: invalid `self` parameter type: isize
18+
error[E0307]: invalid `self` parameter type: `isize`
1919
--> $DIR/ufcs-explicit-self-bad.rs:8:18
2020
|
2121
LL | fn foo(self: isize, x: isize) -> isize {
@@ -24,7 +24,7 @@ LL | fn foo(self: isize, x: isize) -> isize {
2424
= note: type of `self` must be `Self` or a type that dereferences to it
2525
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
2626

27-
error[E0307]: invalid `self` parameter type: Bar<isize>
27+
error[E0307]: invalid `self` parameter type: `Bar<isize>`
2828
--> $DIR/ufcs-explicit-self-bad.rs:19:18
2929
|
3030
LL | fn foo(self: Bar<isize>, x: isize) -> isize {
@@ -33,7 +33,7 @@ LL | fn foo(self: Bar<isize>, x: isize) -> isize {
3333
= note: type of `self` must be `Self` or a type that dereferences to it
3434
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
3535

36-
error[E0307]: invalid `self` parameter type: &Bar<usize>
36+
error[E0307]: invalid `self` parameter type: `&Bar<usize>`
3737
--> $DIR/ufcs-explicit-self-bad.rs:23:18
3838
|
3939
LL | fn bar(self: &Bar<usize>, x: isize) -> isize {

0 commit comments

Comments
 (0)