Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign uponly set non-ADT derive error once per attribute, not per trait #44055
Conversation
rust-highfive
assigned
jseyfried
Aug 23, 2017
oli-obk
reviewed
Aug 23, 2017
| @@ -428,8 +428,8 @@ impl<'a> TraitDef<'a> { | |||
| } | |||
| } | |||
| _ => { | |||
| cx.span_err(mitem.span, | |||
| "`derive` may only be applied to structs, enums and unions"); | |||
| // This not-an-ADT case is an error, but it should have | |||
This comment has been minimized.
This comment has been minimized.
oli-obk
Aug 23, 2017
Contributor
so this branch should be bug!("non-ADT derive should have been caught during expansion")
This comment has been minimized.
This comment has been minimized.
zackmdavis
Aug 26, 2017
Author
Member
Unfortunately, this will ICE: setting the span_err during expansion doesn't stop compilation from reaching this point. (But maybe it should??)
This comment has been minimized.
This comment has been minimized.
estebank
Sep 18, 2017
Contributor
Can you add a debug! statement here then? Also add a comment pointing at libsyntax/ext/expand.rs:MacroExpand::expand() for the handling of this case.
This comment has been minimized.
This comment has been minimized.
zackmdavis
Sep 22, 2017
Author
Member
Can you add a debug! statement here then?
Surprisingly, no: the libsyntax_ext crate doesn't have debug! in scope, and when I tried adding #[macro use] extern crate log; to lib.rs, we get "can't find crate for log". I don't understand what's going on there. (Added the expand filename comment pointer, though.)
This comment has been minimized.
This comment has been minimized.
|
You also need to update some compile-fail tests to your changes.
Click to expand
[00:47:25] [compile-fail] compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs [00:47:25] [compile-fail] compile-fail/feature-gate/issue-43106-gating-of-derive.rs [00:47:25] [compile-fail] compile-fail/issue-36617.rs |
jseyfried
reviewed
Aug 23, 2017
| @@ -282,6 +282,22 @@ impl<'a, 'b> MacroExpander<'a, 'b> { | |||
| let expansion = self.expand_invoc(invoc, ext); | |||
| self.collect_invocations(expansion, &[]) | |||
| } else if let InvocationKind::Attr { attr: None, traits, item } = invoc.kind { | |||
| if let Annotatable::Item(ref item) = item { | |||
This comment has been minimized.
This comment has been minimized.
jseyfried
Aug 23, 2017
Contributor
Could we also error on Annotatable::TraitItem and Annotatable::ImplItem here and then also get rid of the other corresponding error in src/libsyntax_ext/deriving/generic/mod.rs? e.g.
let derive_allowed = match item {
Annotatable::Item(ref item) => match item.node {
ast::ItemKind::Struct(..) | ast::ItemKind::Enum(..) | ast::ItemKind::Union(..) => true,
_ => false,
_ => false,
};
if !derive_allowed {
// error
}
This comment has been minimized.
This comment has been minimized.
|
@zackmdavis looks good! r=me with @oli-obk's comment addressed. Derive expansion is still split somewhat between |
aidanhs
added
the
S-waiting-on-author
label
Aug 23, 2017
zackmdavis
force-pushed the
zackmdavis:condensed_non-ADT_derive_error
branch
from
6a9d0e1
to
9d4c171
Aug 26, 2017
This comment has been minimized.
This comment has been minimized.
|
Updated. Travis error appears spurious (just one macOS subjob failed, and its terminal log says that There are two potential concerns, though:
|
This comment has been minimized.
This comment has been minimized.
|
@zackmdavis Restarted the dummy macOS build, should be green in a few minutes. |
This comment has been minimized.
This comment has been minimized.
|
@zackmdavis do you think we should land this as is, or are you interested in perfecting it? If that, are you still interested in working on this? Just a friendly ping too make sure it isn't getting lost. |
This comment has been minimized.
This comment has been minimized.
|
@arielb1 My disposition is to merge as-is (the reservations bulleted in my previous comment being minor IMO), but if someone were to argue that it's important to preserve the exact error behavior specified in the original compile-fail/issue-43106-gating-of-derive-2.rs, I would respect that judgment, too. |
carols10cents
added
S-waiting-on-review
and removed
S-waiting-on-author
labels
Sep 11, 2017
This comment has been minimized.
This comment has been minimized.
|
@jseyfried sounds like this is in your court for review. what do you think? |
This comment has been minimized.
This comment has been minimized.
|
ping @jseyfried @rust-lang/compiler for review please! |
estebank
reviewed
Sep 18, 2017
|
Other than a few nitpicks I'm r+ too. |
| .find(|attr| attr.check_name("derive")) | ||
| .expect("`derive` attribute should exist").span; | ||
| self.cx.span_err(span, | ||
| "`derive` may only be applied to structs, enums, \ |
This comment has been minimized.
This comment has been minimized.
estebank
Sep 18, 2017
Contributor
Can you keep the existing output and avoid the oxford comma? Not because I'm against it but I'd prefer to keep this change small (so it doesn't affect compile-fail/deriving-non-type.rs at all).
During the impl period I'm going to audit all the existing diagnostic output in order to identify current conventions to write them down. Doing this I fully expect to find inconsistent wording and will try to reach consensus for them.
This comment has been minimized.
This comment has been minimized.
zackmdavis
Sep 22, 2017
Author
Member
I trust that the community will affirm the correctness of the serial comma at that time.
| --> $DIR/issue-43927-non-ADT-derive.rs:13:1 | ||
| | | ||
| 13 | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute! | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
This comment has been minimized.
This comment has been minimized.
estebank
Sep 18, 2017
Contributor
This is a great example of a case where a note pointing out that this derive is being applied to the outer context instead of the struct immediately after would be beneficial. If you don't want to add that on this PR, could you please file a ticket for it?
It could be something along the lines of
error: `derive` may only be applied to structs, enums, and unions
--> $DIR/issue-43927-non-ADT-derive.rs:13:1
|
13 | #![derive(Debug, PartialEq, Eq)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ derive cannot be applied to outer mod
note: as it is written, the derive is applied to the outer scope, did you mean to apply it to the next element?
|
13 | #[derive(Debug, PartialEq, Eq)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This comment has been minimized.
This comment has been minimized.
| @@ -428,8 +428,8 @@ impl<'a> TraitDef<'a> { | |||
| } | |||
| } | |||
| _ => { | |||
| cx.span_err(mitem.span, | |||
| "`derive` may only be applied to structs, enums and unions"); | |||
| // This not-an-ADT case is an error, but it should have | |||
This comment has been minimized.
This comment has been minimized.
estebank
Sep 18, 2017
Contributor
Can you add a debug! statement here then? Also add a comment pointing at libsyntax/ext/expand.rs:MacroExpand::expand() for the handling of this case.
This comment has been minimized.
This comment has been minimized.
|
r=me with @estebank's comment addressed. |
zackmdavis
added some commits
Aug 23, 2017
zackmdavis
force-pushed the
zackmdavis:condensed_non-ADT_derive_error
branch
from
9d4c171
to
083f053
Sep 22, 2017
This comment has been minimized.
This comment has been minimized.
|
(force-pushed)
|
This comment has been minimized.
This comment has been minimized.
|
@bors r=jseyfried |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Sep 23, 2017
This comment has been minimized.
This comment has been minimized.
|
|
bors
merged commit 083f053
into
rust-lang:master
Sep 23, 2017
This comment has been minimized.
This comment has been minimized.
|
|
zackmdavis commentedAug 23, 2017
I found the expansion code very hard to follow, leaving me unsure as to whether this might somehow be done better, but this patch does give us the behavior requested in #43927 (up to exact choice of span; here, it's the entire attribute, not just the
derivetoken).(Note to GitHub robots: resolves #43927.)
r? @jseyfried