Skip to content

Commit dc90573

Browse files
authored
Rollup merge of #79851 - camelid:better-error-for-default-fn, r=davidtwco
Clarify the 'default is only allowed on...' error Code like impl Foo { default fn foo() {} } will trigger the error error: `default` is only allowed on items in `impl` definitions --> src/lib.rs:5:5 | 5 | default fn foo() {} | -------^^^^^^^^^ | | | `default` because of this but that's very confusing! I *did* put it on an item in an impl! So this commit changes the message to error: `default` is only allowed on items in trait impls --> src/lib.rs:5:5 | 5 | default fn foo() {} | -------^^^^^^^^^ | | | `default` because of this
2 parents 17ec4b8 + 4e21942 commit dc90573

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl<'a> AstValidator<'a> {
400400
if let Defaultness::Default(def_span) = defaultness {
401401
let span = self.session.source_map().guess_head_span(span);
402402
self.err_handler()
403-
.struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
403+
.struct_span_err(span, "`default` is only allowed on items in trait impls")
404404
.span_label(def_span, "`default` because of this")
405405
.emit();
406406
}

src/test/ui/parser/trait-item-with-defaultness-fail-semantic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
fn main() {}
44

55
trait X {
6-
default const A: u8; //~ ERROR `default` is only allowed on items in `impl` definitions
7-
default const B: u8 = 0; //~ ERROR `default` is only allowed on items in `impl` definitions
8-
default type D; //~ ERROR `default` is only allowed on items in `impl` definitions
9-
default type C: Ord; //~ ERROR `default` is only allowed on items in `impl` definitions
10-
default fn f1(); //~ ERROR `default` is only allowed on items in `impl` definitions
11-
default fn f2() {} //~ ERROR `default` is only allowed on items in `impl` definitions
6+
default const A: u8; //~ ERROR `default` is only allowed on items in trait impls
7+
default const B: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
8+
default type D; //~ ERROR `default` is only allowed on items in trait impls
9+
default type C: Ord; //~ ERROR `default` is only allowed on items in trait impls
10+
default fn f1(); //~ ERROR `default` is only allowed on items in trait impls
11+
default fn f2() {} //~ ERROR `default` is only allowed on items in trait impls
1212
}

src/test/ui/parser/trait-item-with-defaultness-fail-semantic.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
error: `default` is only allowed on items in `impl` definitions
1+
error: `default` is only allowed on items in trait impls
22
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5
33
|
44
LL | default const A: u8;
55
| -------^^^^^^^^^^^^^
66
| |
77
| `default` because of this
88

9-
error: `default` is only allowed on items in `impl` definitions
9+
error: `default` is only allowed on items in trait impls
1010
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5
1111
|
1212
LL | default const B: u8 = 0;
1313
| -------^^^^^^^^^^^^^^^^^
1414
| |
1515
| `default` because of this
1616

17-
error: `default` is only allowed on items in `impl` definitions
17+
error: `default` is only allowed on items in trait impls
1818
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5
1919
|
2020
LL | default type D;
2121
| -------^^^^^^^^
2222
| |
2323
| `default` because of this
2424

25-
error: `default` is only allowed on items in `impl` definitions
25+
error: `default` is only allowed on items in trait impls
2626
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5
2727
|
2828
LL | default type C: Ord;
2929
| -------^^^^^^^^^^^^^
3030
| |
3131
| `default` because of this
3232

33-
error: `default` is only allowed on items in `impl` definitions
33+
error: `default` is only allowed on items in trait impls
3434
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
3535
|
3636
LL | default fn f1();
3737
| -------^^^^^^^^^
3838
| |
3939
| `default` because of this
4040

41-
error: `default` is only allowed on items in `impl` definitions
41+
error: `default` is only allowed on items in trait impls
4242
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5
4343
|
4444
LL | default fn f2() {}

0 commit comments

Comments
 (0)