add Span to ast::Defaultness::Default.

This commit is contained in:
Mazdak Farrokhzad 2020-02-22 02:01:53 +01:00
parent 6d0e58bff8
commit fa2a792491
11 changed files with 48 additions and 30 deletions

View File

@ -810,13 +810,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
AssocItemKind::Macro(..) => unimplemented!(),
};
hir::TraitItemRef {
id: hir::TraitItemId { hir_id: self.lower_node_id(i.id) },
ident: i.ident,
span: i.span,
defaultness: self.lower_defaultness(Defaultness::Default, has_default),
kind,
}
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
}
/// Construct `ExprKind::Err` for the given `span`.
@ -948,7 +944,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_defaultness(&self, d: Defaultness, has_value: bool) -> hir::Defaultness {
match d {
Defaultness::Default => hir::Defaultness::Default { has_value: has_value },
Defaultness::Default(_) => hir::Defaultness::Default { has_value },
Defaultness::Final => {
assert!(has_value);
hir::Defaultness::Final

View File

@ -400,9 +400,11 @@ impl<'a> AstValidator<'a> {
}
fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
if let Defaultness::Default = defaultness {
if let Defaultness::Default(def_span) = defaultness {
let span = self.session.source_map().def_span(span);
self.err_handler()
.struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
.span_label(def_span, "`default` because of this")
.emit();
}
}
@ -863,10 +865,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
if polarity == ImplPolarity::Negative {
self.err_handler().span_err(item.span, "inherent impls cannot be negative");
}
if defaultness == Defaultness::Default {
if let Defaultness::Default(def_span) = defaultness {
let span = self.session.source_map().def_span(item.span);
self.err_handler()
.struct_span_err(item.span, "inherent impls cannot be default")
.note("only trait implementations may be annotated with default")
.struct_span_err(span, "inherent impls cannot be `default`")
.span_label(def_span, "`default` because of this")
.note("only trait implementations may be annotated with `default`")
.emit();
}
if let Const::Yes(span) = constness {

View File

@ -349,7 +349,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
);
}
if let ast::Defaultness::Default = defaultness {
if let ast::Defaultness::Default(_) = defaultness {
gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
}
}
@ -543,7 +543,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
if i.defaultness == ast::Defaultness::Default {
if let ast::Defaultness::Default(_) = i.defaultness {
gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
}

View File

@ -1389,7 +1389,7 @@ impl<'a> State<'a> {
}
crate fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
if let ast::Defaultness::Default = defaultness {
if let ast::Defaultness::Default(_) = defaultness {
self.word_nbsp("default");
}
}

View File

@ -547,7 +547,7 @@ impl<'a> Parser<'a> {
)
{
self.bump(); // `default`
Defaultness::Default
Defaultness::Default(self.prev_span)
} else {
Defaultness::Final
}

View File

@ -502,7 +502,7 @@ impl Sig for ast::Item {
items: _,
} => {
let mut text = String::new();
if let ast::Defaultness::Default = defaultness {
if let ast::Defaultness::Default(_) = defaultness {
text.push_str("default ");
}
if let ast::Unsafe::Yes(_) = unsafety {

View File

@ -2106,7 +2106,7 @@ pub enum Const {
/// For details see the [RFC #2532](https://github.com/rust-lang/rfcs/pull/2532).
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
pub enum Defaultness {
Default,
Default(Span),
Final,
}

View File

@ -74,13 +74,17 @@ error: `default` is only allowed on items in `impl` definitions
--> $DIR/assoc-static-semantic-fail.rs:24:5
|
LL | default static TC: u8 = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| -------^^^^^^^^^^^^^^^^^^^
| |
| `default` because of this
error: `default` is only allowed on items in `impl` definitions
--> $DIR/assoc-static-semantic-fail.rs:27:5
|
LL | pub(crate) default static TD: u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^-------^^^^^^^^^^^^^^^
| |
| `default` because of this
error[E0449]: unnecessary visibility qualifier
--> $DIR/assoc-static-semantic-fail.rs:27:5

View File

@ -2,37 +2,49 @@ error: `default` is only allowed on items in `impl` definitions
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5
|
LL | default const A: u8;
| ^^^^^^^^^^^^^^^^^^^^
| -------^^^^^^^^^^^^^
| |
| `default` because of this
error: `default` is only allowed on items in `impl` definitions
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5
|
LL | default const B: u8 = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^
| -------^^^^^^^^^^^^^^^^^
| |
| `default` because of this
error: `default` is only allowed on items in `impl` definitions
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5
|
LL | default type D;
| ^^^^^^^^^^^^^^^
| -------^^^^^^^^
| |
| `default` because of this
error: `default` is only allowed on items in `impl` definitions
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5
|
LL | default type C: Ord;
| ^^^^^^^^^^^^^^^^^^^^
| -------^^^^^^^^^^^^^
| |
| `default` because of this
error: `default` is only allowed on items in `impl` definitions
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
|
LL | default fn f1();
| ^^^^^^^^^^^^^^^^
| -------^^^^^^^^^
| |
| `default` because of this
error: `default` is only allowed on items in `impl` definitions
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5
|
LL | default fn f2() {}
| ^^^^^^^^^^^^^^^^^^
| -------^^^^^^^^
| |
| `default` because of this
error: aborting due to 6 previous errors

View File

@ -4,7 +4,7 @@
struct S;
struct Z;
default impl S {} //~ ERROR inherent impls cannot be default
default impl S {} //~ ERROR inherent impls cannot be `default`
default unsafe impl Send for S {} //~ ERROR impls of auto traits cannot be default
default impl !Send for Z {} //~ ERROR impls of auto traits cannot be default

View File

@ -1,10 +1,12 @@
error: inherent impls cannot be default
error: inherent impls cannot be `default`
--> $DIR/validation.rs:7:1
|
LL | default impl S {}
| ^^^^^^^^^^^^^^^^^
| -------^^^^^^^
| |
| `default` because of this
|
= note: only trait implementations may be annotated with default
= note: only trait implementations may be annotated with `default`
error: impls of auto traits cannot be default
--> $DIR/validation.rs:9:1