Skip to content

only set non-ADT derive error once per attribute, not per trait #44055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2017

Conversation

zackmdavis
Copy link
Member

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 derive token).

(Note to GitHub robots: resolves #43927.)

r? @jseyfried

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this branch should be bug!("non-ADT derive should have been caught during expansion")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this will ICE: setting the span_err during expansion doesn't stop compilation from reaching this point. (But maybe it should??)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

@oli-obk
Copy link
Contributor

oli-obk commented Aug 23, 2017

You also need to update some compile-fail tests to your changes.

Click to expand

[00:47:25] [compile-fail] compile-fail/deriving-non-type.rs
[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

[00:47:25] failures:
[00:47:25] 
[00:47:25] ---- [compile-fail] compile-fail/deriving-non-type.rs stdout ----
[00:47:25] 	
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:15: unexpected "error": '15:1: 15:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:18: unexpected "error": '18:1: 18:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:21: unexpected "error": '21:1: 21:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:24: unexpected "error": '24:1: 24:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:27: unexpected "error": '27:1: 27:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:30: unexpected "error": '30:1: 30:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:33: unexpected "error": '33:1: 33:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:36: unexpected "error": '36:1: 36:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:39: unexpected "error": '39:1: 39:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:15: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:18: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:21: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:24: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:27: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:30: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:33: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:36: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/deriving-non-type.rs:39: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: 9 unexpected errors found, 9 expected errors not found
[00:47:25] status: exit code: 101
[00:47:25] command: /checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /checkout/src/test/compile-fail/deriving-non-type.rs -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail --target=x86_64-unknown-linux-gnu --error-format json -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/deriving-non-type.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -A unused -C prefer-dynamic -o /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/deriving-non-type.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers
[00:47:25] unexpected errors (from JSON output): [
[00:47:25]     Error {
[00:47:25]         line_num: 15,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "15:1: 15:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 18,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "18:1: 18:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 21,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "21:1: 21:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 24,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "24:1: 24:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 27,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "27:1: 27:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 30,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "30:1: 30:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 33,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "33:1: 33:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 36,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "36:1: 36:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 39,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "39:1: 39:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] not found errors (from test file): [
[00:47:25]     Error {
[00:47:25]         line_num: 15,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 18,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 21,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 24,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 27,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 30,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 33,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 36,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 39,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] thread '[compile-fail] compile-fail/deriving-non-type.rs' panicked at 'explicit panic', /checkout/src/tools/compiletest/src/runtest.rs:1133:12
[00:47:25] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[00:47:25] 
[00:47:25] ---- [compile-fail] compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs stdout ----
[00:47:25] 	
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:15: unexpected "error": '15:1: 15:18: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:18: unexpected "error": '18:1: 18:17: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:21: unexpected "error": '21:17: 21:34: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:24: unexpected "error": '24:5: 24:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:40: unexpected "error": '40:5: 40:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:44: unexpected "error": '44:5: 44:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:15: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:18: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:21: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:24: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:28: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:32: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:36: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:40: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs:44: expected error not found: cannot find derive macro `x3300` in this scope
[00:47:25] 
[00:47:25] error: 6 unexpected errors found, 9 expected errors not found
[00:47:25] status: exit code: 101
[00:47:25] command: /checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail --target=x86_64-unknown-linux-gnu --error-format json -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -A unused -C prefer-dynamic -o /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/feature-gate/issue-43106-gating-of-derive-2.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers
[00:47:25] unexpected errors (from JSON output): [
[00:47:25]     Error {
[00:47:25]         line_num: 15,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "15:1: 15:18: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 18,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "18:1: 18:17: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 21,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "21:17: 21:34: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 24,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "24:5: 24:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 40,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "40:5: 40:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 44,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "44:5: 44:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] not found errors (from test file): [
[00:47:25]     Error {
[00:47:25]         line_num: 15,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 18,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 21,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 24,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 28,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 32,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 36,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 40,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 44,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "cannot find derive macro `x3300` in this scope"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] thread '[compile-fail] compile-fail/feature-gate/issue-43106-gating-of-derive-2.rs' panicked at 'explicit panic', /checkout/src/tools/compiletest/src/runtest.rs:1133:12
[00:47:25] 
[00:47:25] ---- [compile-fail] compile-fail/feature-gate/issue-43106-gating-of-derive.rs stdout ----
[00:47:25] 	
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:15: unexpected "error": '15:1: 15:18: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:18: unexpected "error": '18:1: 18:17: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:21: unexpected "error": '21:17: 21:34: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:24: unexpected "error": '24:5: 24:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:37: unexpected "error": '37:5: 37:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:41: unexpected "error": '41:5: 41:21: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:15: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:18: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:21: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:24: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:37: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs:41: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: 6 unexpected errors found, 6 expected errors not found
[00:47:25] status: exit code: 101
[00:47:25] command: /checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /checkout/src/test/compile-fail/feature-gate/issue-43106-gating-of-derive.rs -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail --target=x86_64-unknown-linux-gnu --error-format json -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/feature-gate/issue-43106-gating-of-derive.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -A unused -C prefer-dynamic -o /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/feature-gate/issue-43106-gating-of-derive.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers
[00:47:25] unexpected errors (from JSON output): [
[00:47:25]     Error {
[00:47:25]         line_num: 15,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "15:1: 15:18: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 18,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "18:1: 18:17: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 21,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "21:17: 21:34: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 24,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "24:5: 24:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 37,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "37:5: 37:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 41,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "41:5: 41:21: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     }
[00:47:25] thread 'main' panicked at 'Some tests failed', /checkout/src/tools/compiletest/src/main.rs:323:21
[00:47:25] ]
[00:47:25] 
[00:47:25] not found errors (from test file): [
[00:47:25]     Error {
[00:47:25]         line_num: 15,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 18,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 21,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 24,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 37,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     },
[00:47:25]     Error {
[00:47:25]         line_num: 41,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] thread '[compile-fail] compile-fail/feature-gate/issue-43106-gating-of-derive.rs' panicked at 'explicit panic', /checkout/src/tools/compiletest/src/runtest.rs:1133:12
[00:47:25] 
[00:47:25] ---- [compile-fail] compile-fail/issue-36617.rs stdout ----
[00:47:25] 	
[00:47:25] error: /checkout/src/test/compile-fail/issue-36617.rs:11: unexpected "error": '11:1: 11:17: `derive` may only be applied to structs, enums, and unions'
[00:47:25] 
[00:47:25] error: /checkout/src/test/compile-fail/issue-36617.rs:11: expected error not found: `derive` may only be applied to structs, enums and unions
[00:47:25] 
[00:47:25] error: 1 unexpected errors found, 1 expected errors not found
[00:47:25] status: exit code: 101
[00:47:25] command: /checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /checkout/src/test/compile-fail/issue-36617.rs -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail --target=x86_64-unknown-linux-gnu --error-format json -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/issue-36617.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -A unused -C prefer-dynamic -o /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail/issue-36617.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers
[00:47:25] unexpected errors (from JSON output): [
[00:47:25]     Error {
[00:47:25]         line_num: 11,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "11:1: 11:17: `derive` may only be applied to structs, enums, and unions"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] not found errors (from test file): [
[00:47:25]     Error {
[00:47:25]         line_num: 11,
[00:47:25]         kind: Some(
[00:47:25]             Error
[00:47:25]         ),
[00:47:25]         msg: "`derive` may only be applied to structs, enums and unions"
[00:47:25]     }
[00:47:25] ]
[00:47:25] 
[00:47:25] thread '[compile-fail] compile-fail/issue-36617.rs' panicked at 'explicit panic', /checkout/src/tools/compiletest/src/runtest.rs:1133:12
[00:47:25] 
[00:47:25] 
[00:47:25] failures:
[00:47:25]     [compile-fail] compile-fail/deriving-non-type.rs
[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
[00:47:25] 
[00:47:25] test result: �[31mFAILED�(B�[m. 2715 passed; 4 failed; 13 ignored; 0 measured; 0 filtered out

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

@jseyfried
Copy link
Contributor

jseyfried commented Aug 23, 2017

@zackmdavis looks good! r=me with @oli-obk's comment addressed.

Derive expansion is still split somewhat between libsyntax and libsyntax_ext so it can be confusing. We're trying to move the expansion logic out of libsyntax_ext to libsyntax; this PR is done well and moves us in the right direction :)

@aidanhs aidanhs added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 23, 2017
@zackmdavis zackmdavis force-pushed the condensed_non-ADT_derive_error branch from 6a9d0e1 to 9d4c171 Compare August 26, 2017 22:24
@zackmdavis
Copy link
Member Author

Updated. Travis error appears spurious (just one macOS subjob failed, and its terminal log says that git fetch failed).

There are two potential concerns, though:

  • The code in libsyntax_ext/deriving/generic/mod.rs still runs, so we can't put an assertion there unless we somehow stop compilation earlier.
  • As revealed by the compile-fail tests, if the non-ADT-derive error occurs, then we don't get to see any derive-macro-not-found errors (which will surface the next time the user attempts to compile after fixing the erroneous non-ADT derives). This isn't necessarily a blocker (it's pretty ordinary for errors in one pass to overshadow others), but it wasn't intended and I don't understand why it's happening.

@eddyb
Copy link
Member

eddyb commented Aug 30, 2017

@zackmdavis Restarted the dummy macOS build, should be green in a few minutes.

@arielb1
Copy link
Contributor

arielb1 commented Sep 5, 2017

@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.

@zackmdavis
Copy link
Member Author

@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 carols10cents added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 11, 2017
@carols10cents
Copy link
Member

@jseyfried sounds like this is in your court for review. what do you think?

@carols10cents
Copy link
Member

ping @jseyfried @rust-lang/compiler for review please!

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

083f053

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jseyfried
Copy link
Contributor

jseyfried commented Sep 20, 2017

r=me with @estebank's comment addressed.

A slight eccentricity of this change is that now non-ADT-derive errors prevent
derive-macro-not-found errors from surfacing (see changes to the
gating-of-derive compile-fail tests).

Resolves rust-lang#43927.
@zackmdavis zackmdavis force-pushed the condensed_non-ADT_derive_error branch from 9d4c171 to 083f053 Compare September 22, 2017 04:21
@zackmdavis
Copy link
Member Author

zackmdavis commented Sep 22, 2017

(force-pushed)

  • We no longer add a serial comma to the message, leaving that to a future error-message auditing pass.
  • "error set earlier" comments in libsyntax_ext note file/function where
    • unfortunately, the debug! macro isn't available in libsyntax_ext (and trying to add the log crate didn't work), so the reviewer suggestion to use that was not taken in this revision
  • another commit adds a suggestion to use an outer attribute (#, not #!) in the case where the offending attribute was an inner

@estebank
Copy link
Contributor

@bors r=jseyfried

@bors
Copy link
Collaborator

bors commented Sep 23, 2017

📌 Commit 083f053 has been approved by jseyfried

@bors
Copy link
Collaborator

bors commented Sep 23, 2017

⌛ Testing commit 083f053 with merge 9ad67e9...

bors added a commit that referenced this pull request Sep 23, 2017
…seyfried

only set non-ADT derive error once per attribute, not per trait

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 `derive` token).

(Note to GitHub robots: _resolves #43927_.)

r? @jseyfried
@bors
Copy link
Collaborator

bors commented Sep 23, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: jseyfried
Pushing 9ad67e9 to master...

@bors bors merged commit 083f053 into rust-lang:master Sep 23, 2017
@Havvy
Copy link
Contributor

Havvy commented Sep 23, 2017

💟

@zackmdavis zackmdavis deleted the condensed_non-ADT_derive_error branch January 13, 2018 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

derive may only be applied to structs, enums and unions error should only show once per derive
10 participants