Skip to content
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

Point macros 1.1 errors to the input item #36308

Merged
merged 3 commits into from
Sep 12, 2016
Merged

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Sep 6, 2016

Moved from alexcrichton#6 to continue discussion. Fixes #36218.

Before:

error[E0106]: missing lifetime specifier
  --> src/main.rs:10:10
   |
10 | #[derive(Serialize, Deserialize)]
   |          ^ expected lifetime parameter

error[E0038]: the trait `T` cannot be made into an object
  --> src/main.rs:15:15
   |
15 | #[derive(Serialize, Deserialize)]
   |          ^^^^^^^^^^ the trait `T` cannot be made into an object

After:

error[E0106]: missing lifetime specifier
  --> src/main.rs:11:1
   |
11 | struct A {
   | ^ expected lifetime parameter

error[E0038]: the trait `T` cannot be made into an object
  --> src/main.rs:16:1
   |
16 | struct B<'a> {
   | ^ the trait `T` cannot be made into an object

Before:

```rust
error[E0106]: missing lifetime specifier
  --> src/main.rs:10:10
   |
10 | #[derive(Serialize, Deserialize)]
   |          ^ expected lifetime parameter

error[E0038]: the trait `T` cannot be made into an object
  --> src/main.rs:15:15
   |
15 | #[derive(Serialize, Deserialize)]
   |          ^^^^^^^^^^ the trait `T` cannot be made into an object
```

After:

```rust
error[E0106]: missing lifetime specifier
  --> src/main.rs:11:1
   |
11 | struct A {
   | ^ expected lifetime parameter

error[E0038]: the trait `T` cannot be made into an object
  --> src/main.rs:16:1
   |
16 | struct B<'a> {
   | ^ the trait `T` cannot be made into an object
```
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@alexcrichton
Copy link
Member

Thanks @dtolnay! I wonder if perhaps the error could also somehow mention that the code originally came from #[derive]? At this point I'm not too worried about being principled here, I think it's fine to basically choose whatever is the most understandable.

Also looks like there are some errors on travis:

failures:

---- [compile-fail] compile-fail/rustc-macro/append-impl.rs stdout ----

error: /build/src/test/compile-fail-fulldeps/rustc-macro/append-impl.rs:27: unexpected "error": '27:1: 29:2: the semantics of constant patterns is not yet settled (see issue #31434)'

error: /build/src/test/compile-fail-fulldeps/rustc-macro/append-impl.rs:24: expected error not found: the semantics of constant patterns is not yet settled

error: 1 unexpected errors found, 1 expected errors not found
status: exit code: 101
command: /build/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /build/src/test/compile-fail-fulldeps/rustc-macro/append-impl.rs -L /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps --target=x86_64-unknown-linux-gnu --error-format json -L /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/rustc-macro/append-impl.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -C prefer-dynamic -o /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/rustc-macro/append-impl.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/build/build/x86_64-unknown-linux-gnu/rust-test-helpers
unexpected errors (from JSON output): [
    Error {
        line_num: 27,
        kind: Some(
            Error
        ),
        msg: "27:1: 29:2: the semantics of constant patterns is not yet settled (see issue #31434)"
    }
]

not found errors (from test file): [
    Error {
        line_num: 24,
        kind: Some(
            Error
        ),
        msg: "the semantics of constant patterns is not yet settled"
    }
]

thread '[compile-fail] compile-fail/rustc-macro/append-impl.rs' panicked at 'explicit panic', src/tools/compiletest/src/runtest.rs:1084
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- [compile-fail] compile-fail/rustc-macro/expand-to-unstable-2.rs stdout ----

error: /build/src/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable-2.rs:21: unexpected "error": '21:1: 21:10: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics (see issue #29642)'

error: /build/src/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable-2.rs:19: expected error not found: reserved for internal compiler

error: 1 unexpected errors found, 1 expected errors not found
status: exit code: 101
command: /build/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /build/src/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable-2.rs -L /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps --target=x86_64-unknown-linux-gnu --error-format json -L /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable-2.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -C prefer-dynamic -o /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable-2.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/build/build/x86_64-unknown-linux-gnu/rust-test-helpers
unexpected errors (from JSON output): [
    Error {
        line_num: 21,
        kind: Some(
            Error
        ),
        msg: "21:1: 21:10: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics (see issue #29642)"
    }
]

not found errors (from test file): [
    Error {
        line_num: 19,
        kind: Some(
            Error
        ),
        msg: "reserved for internal compiler"
    }
]

thread '[compile-fail] compile-fail/rustc-macro/expand-to-unstable-2.rs' panicked at 'explicit panic', src/tools/compiletest/src/runtest.rs:1084

---- [compile-fail] compile-fail/rustc-macro/expand-to-unstable.rs stdout ----

error: /build/src/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable.rs:21: unexpected "error": '21:1: 21:10: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library (see issue #0)'

error: /build/src/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable.rs:19: expected error not found: use of unstable library feature

error: 1 unexpected errors found, 1 expected errors not found
status: exit code: 101
command: /build/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /build/src/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable.rs -L /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps --target=x86_64-unknown-linux-gnu --error-format json -L /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -C prefer-dynamic -o /build/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/rustc-macro/expand-to-unstable.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/build/build/x86_64-unknown-linux-gnu/rust-test-helpers
unexpected errors (from JSON output): [
    Error {
        line_num: 21,
        kind: Some(
            Error
        ),
        msg: "21:1: 21:10: use of unstable library feature \'core_intrinsics\': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library (see issue #0)"
    }
]

not found errors (from test file): [
    Error {
        line_num: 19,
        kind: Some(
            Error
        ),
        msg: "use of unstable library feature"
    }
]

thread '[compile-fail] compile-fail/rustc-macro/expand-to-unstable.rs' panicked at 'explicit panic', src/tools/compiletest/src/runtest.rs:1084


failures:
    [compile-fail] compile-fail/rustc-macro/append-impl.rs
    [compile-fail] compile-fail/rustc-macro/expand-to-unstable-2.rs
    [compile-fail] compile-fail/rustc-macro/expand-to-unstable.rs

test result: FAILED. 43 passed; 3 failed; 0 ignored; 0 measured

@dtolnay
Copy link
Member Author

dtolnay commented Sep 8, 2016

I wonder if perhaps the error could also somehow mention that the code originally came from #[derive]?

I tried the following:

let input_span = Span {
    expn_id: ecx.codemap().record_expansion(ExpnInfo {
        call_site: span,
        callee: NameAndSpan {
            format: MacroAttribute(intern(&pprust::meta_item_to_string(meta_item))),
            span: Some(span),
            allow_internal_unstable: true,
        },
    }),
    ..item.span
};

expecting to see "note: in this macro invocation" pointing to the derive Serialize, but it didn't show that so I'll leave it to you. I will update the tests.

@alexcrichton
Copy link
Member

Thanks! I talked with @nrc offline a bit and the conclusion was:

  • Ideally this span would point to the item and the attribute which was input to the #[derive], but for now just the item is fine.
  • We need to make sure that the span of the expanded code has some expn_id which indicates that it's expanded.

I think that the latter may already be true though? @dtolnay could you check to see if it's the case?

@dtolnay
Copy link
Member Author

dtolnay commented Sep 9, 2016

Yep, working on confirming now. Side note: it would have helped if -Z ast-json contained expn ids.

@dtolnay
Copy link
Member Author

dtolnay commented Sep 10, 2016

No, unfortunately the expanded code doesn't already have an expn_id. I tried the following:

struct A;

#[derive(Identity)]
struct B;

macro_rules! c {
    () => {
        struct C;
    };
}
c!();

where Identity is:

#[rustc_macro_derive(Identity)]
pub fn id(input: TokenStream) -> TokenStream {
    input
}

The expanded code of course looks like:

struct A;

struct B;

struct C;

As expected, A has an expn_id of NO_EXPANSION and C has a small positive expn_id (I got 3). B has an expn_id of NO_EXPANSION.

@dtolnay
Copy link
Member Author

dtolnay commented Sep 10, 2016

I pushed a commit that adds expn_ids. In my example the B became expn_id=2 and C became expn_id=4.

@alexcrichton
Copy link
Member

@bors: r+ fe41520

Thanks!

@bors
Copy link
Contributor

bors commented Sep 10, 2016

⌛ Testing commit fe41520 with merge d9a533f...

@bors
Copy link
Contributor

bors commented Sep 10, 2016

💔 Test failed - auto-win-msvc-64-cargotest

@alexcrichton
Copy link
Member

@bors: retry

On Sat, Sep 10, 2016 at 11:47 AM, bors notifications@github.com wrote:

💔 Test failed - auto-win-msvc-64-cargotest
https://buildbot.rust-lang.org/builders/auto-win-msvc-64-cargotest/builds/1783


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#36308 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAD95HPQ2mOeHZigt07T1-AseORa7tVZks5qovs5gaJpZM4J2Sp3
.

@bors
Copy link
Contributor

bors commented Sep 11, 2016

⌛ Testing commit fe41520 with merge 0be88eb...

bors added a commit that referenced this pull request Sep 11, 2016
Point macros 1.1 errors to the input item

Moved from alexcrichton#6 to continue discussion. Fixes #36218.

Before:

```rust
error[E0106]: missing lifetime specifier
  --> src/main.rs:10:10
   |
10 | #[derive(Serialize, Deserialize)]
   |          ^ expected lifetime parameter

error[E0038]: the trait `T` cannot be made into an object
  --> src/main.rs:15:15
   |
15 | #[derive(Serialize, Deserialize)]
   |          ^^^^^^^^^^ the trait `T` cannot be made into an object
```

After:

```rust
error[E0106]: missing lifetime specifier
  --> src/main.rs:11:1
   |
11 | struct A {
   | ^ expected lifetime parameter

error[E0038]: the trait `T` cannot be made into an object
  --> src/main.rs:16:1
   |
16 | struct B<'a> {
   | ^ the trait `T` cannot be made into an object
```
@SimonSapin
Copy link
Contributor

So, uh, I filed #37563 to ask for the exact opposite change.

I got an "unreachable expression" warning with the span pointing to an (empty) enum declaration. This made no sense to me, there is no expression there, it’s a type! It did not even occur to me that the error could be in code generated by a custom derive, until I asked for help on IRC and someone suggested it.

@dtolnay
Copy link
Member Author

dtolnay commented Nov 4, 2016

Responded in #37563 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants