Skip to content

Commit fa2a792

Browse files
committed
add Span to ast::Defaultness::Default.
1 parent 6d0e58b commit fa2a792

File tree

11 files changed

+48
-30
lines changed

11 files changed

+48
-30
lines changed

src/librustc_ast_lowering/item.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -810,13 +810,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
810810
}
811811
AssocItemKind::Macro(..) => unimplemented!(),
812812
};
813-
hir::TraitItemRef {
814-
id: hir::TraitItemId { hir_id: self.lower_node_id(i.id) },
815-
ident: i.ident,
816-
span: i.span,
817-
defaultness: self.lower_defaultness(Defaultness::Default, has_default),
818-
kind,
819-
}
813+
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
814+
let defaultness = hir::Defaultness::Default { has_value: has_default };
815+
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
820816
}
821817

822818
/// Construct `ExprKind::Err` for the given `span`.
@@ -948,7 +944,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
948944

949945
fn lower_defaultness(&self, d: Defaultness, has_value: bool) -> hir::Defaultness {
950946
match d {
951-
Defaultness::Default => hir::Defaultness::Default { has_value: has_value },
947+
Defaultness::Default(_) => hir::Defaultness::Default { has_value },
952948
Defaultness::Final => {
953949
assert!(has_value);
954950
hir::Defaultness::Final

src/librustc_ast_passes/ast_validation.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,11 @@ impl<'a> AstValidator<'a> {
400400
}
401401

402402
fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
403-
if let Defaultness::Default = defaultness {
403+
if let Defaultness::Default(def_span) = defaultness {
404+
let span = self.session.source_map().def_span(span);
404405
self.err_handler()
405406
.struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
407+
.span_label(def_span, "`default` because of this")
406408
.emit();
407409
}
408410
}
@@ -863,10 +865,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
863865
if polarity == ImplPolarity::Negative {
864866
self.err_handler().span_err(item.span, "inherent impls cannot be negative");
865867
}
866-
if defaultness == Defaultness::Default {
868+
if let Defaultness::Default(def_span) = defaultness {
869+
let span = self.session.source_map().def_span(item.span);
867870
self.err_handler()
868-
.struct_span_err(item.span, "inherent impls cannot be default")
869-
.note("only trait implementations may be annotated with default")
871+
.struct_span_err(span, "inherent impls cannot be `default`")
872+
.span_label(def_span, "`default` because of this")
873+
.note("only trait implementations may be annotated with `default`")
870874
.emit();
871875
}
872876
if let Const::Yes(span) = constness {

src/librustc_ast_passes/feature_gate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
349349
);
350350
}
351351

352-
if let ast::Defaultness::Default = defaultness {
352+
if let ast::Defaultness::Default(_) = defaultness {
353353
gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
354354
}
355355
}
@@ -543,7 +543,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
543543
}
544544

545545
fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
546-
if i.defaultness == ast::Defaultness::Default {
546+
if let ast::Defaultness::Default(_) = i.defaultness {
547547
gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
548548
}
549549

src/librustc_ast_pretty/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ impl<'a> State<'a> {
13891389
}
13901390

13911391
crate fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
1392-
if let ast::Defaultness::Default = defaultness {
1392+
if let ast::Defaultness::Default(_) = defaultness {
13931393
self.word_nbsp("default");
13941394
}
13951395
}

src/librustc_parse/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ impl<'a> Parser<'a> {
547547
)
548548
{
549549
self.bump(); // `default`
550-
Defaultness::Default
550+
Defaultness::Default(self.prev_span)
551551
} else {
552552
Defaultness::Final
553553
}

src/librustc_save_analysis/sig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl Sig for ast::Item {
502502
items: _,
503503
} => {
504504
let mut text = String::new();
505-
if let ast::Defaultness::Default = defaultness {
505+
if let ast::Defaultness::Default(_) = defaultness {
506506
text.push_str("default ");
507507
}
508508
if let ast::Unsafe::Yes(_) = unsafety {

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ pub enum Const {
21062106
/// For details see the [RFC #2532](https://github.com/rust-lang/rfcs/pull/2532).
21072107
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
21082108
pub enum Defaultness {
2109-
Default,
2109+
Default(Span),
21102110
Final,
21112111
}
21122112

src/test/ui/parser/assoc-static-semantic-fail.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ error: `default` is only allowed on items in `impl` definitions
7474
--> $DIR/assoc-static-semantic-fail.rs:24:5
7575
|
7676
LL | default static TC: u8 = 0;
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
77+
| -------^^^^^^^^^^^^^^^^^^^
78+
| |
79+
| `default` because of this
7880

7981
error: `default` is only allowed on items in `impl` definitions
8082
--> $DIR/assoc-static-semantic-fail.rs:27:5
8183
|
8284
LL | pub(crate) default static TD: u8;
83-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85+
| ^^^^^^^^^^^-------^^^^^^^^^^^^^^^
86+
| |
87+
| `default` because of this
8488

8589
error[E0449]: unnecessary visibility qualifier
8690
--> $DIR/assoc-static-semantic-fail.rs:27:5

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@ error: `default` is only allowed on items in `impl` definitions
22
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5
33
|
44
LL | default const A: u8;
5-
| ^^^^^^^^^^^^^^^^^^^^
5+
| -------^^^^^^^^^^^^^
6+
| |
7+
| `default` because of this
68

79
error: `default` is only allowed on items in `impl` definitions
810
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5
911
|
1012
LL | default const B: u8 = 0;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
13+
| -------^^^^^^^^^^^^^^^^^
14+
| |
15+
| `default` because of this
1216

1317
error: `default` is only allowed on items in `impl` definitions
1418
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5
1519
|
1620
LL | default type D;
17-
| ^^^^^^^^^^^^^^^
21+
| -------^^^^^^^^
22+
| |
23+
| `default` because of this
1824

1925
error: `default` is only allowed on items in `impl` definitions
2026
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5
2127
|
2228
LL | default type C: Ord;
23-
| ^^^^^^^^^^^^^^^^^^^^
29+
| -------^^^^^^^^^^^^^
30+
| |
31+
| `default` because of this
2432

2533
error: `default` is only allowed on items in `impl` definitions
2634
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
2735
|
2836
LL | default fn f1();
29-
| ^^^^^^^^^^^^^^^^
37+
| -------^^^^^^^^^
38+
| |
39+
| `default` because of this
3040

3141
error: `default` is only allowed on items in `impl` definitions
3242
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5
3343
|
3444
LL | default fn f2() {}
35-
| ^^^^^^^^^^^^^^^^^^
45+
| -------^^^^^^^^
46+
| |
47+
| `default` because of this
3648

3749
error: aborting due to 6 previous errors
3850

src/test/ui/specialization/defaultimpl/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
struct S;
55
struct Z;
66

7-
default impl S {} //~ ERROR inherent impls cannot be default
7+
default impl S {} //~ ERROR inherent impls cannot be `default`
88

99
default unsafe impl Send for S {} //~ ERROR impls of auto traits cannot be default
1010
default impl !Send for Z {} //~ ERROR impls of auto traits cannot be default

0 commit comments

Comments
 (0)