Skip to content

Importing suggestion can add use-statement in between attribute and struct #3237

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

Closed
Nashenas88 opened this issue Feb 19, 2020 · 23 comments
Closed

Comments

@Nashenas88
Copy link
Contributor

I'm working on a proc-macro crate, and I was trying to import the use statement for Ident (on line 8 below):

#[derive(Copy, Clone)]
pub struct Symbol(&'static str);

pub const CRATE: Symbol = Symbol("crate");
pub const PROVIDES: Symbol = Symbol("provides");
pub const WITH: Symbol = Symbol("with");

impl PartialEq<Symbol> for Ident {
}

In vs code, I hit Ctrl+. to bring up the suggestions, where I selected "possible candidates are found in other modules, you can import them into scope: 'use proc_macro2::Ident;\n\n'". This generates the following:

#[derive(Copy, Clone)]
use proc_macro2::Ident;

pub struct Symbol(&'static str);

pub const CRATE: Symbol = Symbol("crate");
pub const PROVIDES: Symbol = Symbol("provides");
pub const WITH: Symbol = Symbol("with");

impl PartialEq<Symbol> for Ident {   
}

The use proc_macro2::Ident; line is inserted between the attribute and the struct, causing the attribute to now apply to the use statement. This should obviously be corrected to:

use proc_macro2::Ident;

#[derive(Copy, Clone)]
pub struct Symbol(&'static str);

pub const CRATE: Symbol = Symbol("crate");
pub const PROVIDES: Symbol = Symbol("provides");
pub const WITH: Symbol = Symbol("with");

impl PartialEq<Symbol> for Ident {
}

Versions:

rust-analyzer: commit eab80cd
vs code: 1.42.1
uname -a: Linux pop-os 5.3.0-7625-generic #27~1576774560~19.10~f432cd8-Ubuntu SMP Thu Dec 19 20:35:37 UTC x86_64 x86_64 x86_64 GNU/Linux

@matklad
Copy link
Member

matklad commented Feb 19, 2020

cc @SomeoneToIgnore

@SomeoneToIgnore
Copy link
Contributor

Oh, that massive insert_use_statement needs more love: another test and a fix.

To be more presize, add_after_anchor-related logic needs more tweaks here:
https://github.com/rust-analyzer/rust-analyzer/blob/5bf669860984a2c058b3bdc3e43b4993a0f25b31/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs#L476

Thank you for the report, I'll try to look at it this week, if nobody does this before.

@SomeoneToIgnore
Copy link
Contributor

@Nashenas88 , I think we're missing some bits of the information here.
I have the following setup:

image

and get the import placed on the top of the file:

image

This sentence sounds a bit suspicious:

where I selected "possible candidates are found in other modules, you can import them into scope: 'use proc_macro2::Ident;\n\n'"

Are you sure that you're using rust-analyzer functionality to import the struct?
rust-analyzer has a completely different way of proposing an import:

image

No words about "possible candidates", "modules", no "use" word either.

@bjorn3
Copy link
Member

bjorn3 commented Feb 19, 2020

Could it have been suggested by rustc?

@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Feb 19, 2020

The message looks exactly as if it was, but rustc does not add imports, does it?

I'm rebuilding RA on Linux just in case the platfor has to do something with the bug.

@bjorn3
Copy link
Member

bjorn3 commented Feb 19, 2020

Rustc can give suggestions for unresolved idents. I think they are marked as machine applicable.

@Nashenas88 do you have the RLS extension installed in addition to rust-analyzer?

@SomeoneToIgnore
Copy link
Contributor

I was able to get the compiler message with the same words (when fixed my set up to a proper one: moved the code into a foo.rs and added a proc macro extern crate into lib.rs), but still no extra proposals from RA, not even on Linux.

And I don't have rls installed, so maybe that's the case indeed.

@SomeoneToIgnore
Copy link
Contributor

I'm going to close this since the issue seems not to be related to RA and we've got no extra details to think otherwise or to continue the investigation.

Feel free to comment in case something new appears on the topic.

@Nashenas88
Copy link
Contributor Author

@bjorn3 @SomeoneToIgnore I don't have RLS installed, only Rust Analyzer. The error does indeed come from the compiler itself, since when compiling from the terminal, I get this output:

error[E0412]: cannot find type `Ident` in this scope
  --> coi-derive/src/symbol.rs:15:28
   |
15 | impl PartialEq<Symbol> for Ident {
   |                            ^^^^^ not found in this scope
   |
help: possible candidates are found in other modules, you can import them into scope
   |
6  | use proc_macro2::Ident;
   |
6  | use proc_macro::Ident;
   |
6  | use proc_macro::bridge::server::Ident;
   |
6  | use quote::__rt::Ident;
   |
     and 1 other candidate

Does something in RA try to apply rustc suggestions? I did used to have RLS installed, but I would be surprised if uninstalling it left context menu actions around. The only other extensions I have that might apply are ones limited to cargo and crates.io, but to my understanding those are for Cargo.toml file only.

@Nashenas88
Copy link
Contributor Author

To clarify, the only extension I have that I expect to work on rust source code is rust-analyzer.

@Nashenas88
Copy link
Contributor Author

If it helps, this is my list of currently installed extensions. The one that's cut off at the top is Better TOML.

Screenshot from 2020-02-25 09-26-38

@kiljacken
Copy link
Contributor

Rust-analyzer does provide actions for machine-applicable fixes from rustc. I'm not sure if this is an upstream bug or an off-by-one on our line count. Could you attach the output of cargo check --message-format json while the error is occuring? That should help us narrow it down :)

@kiljacken kiljacken reopened this Feb 25, 2020
@Nashenas88
Copy link
Contributor Author

Here's the result of that command:

{"reason":"compiler-artifact","package_id":"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["custom-build"],"crate_types":["bin"],"name":"build-script-build","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/proc-macro2-1.0.8/build.rs","edition":"2018","doctest":false},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["default","proc-macro"],"filenames":["/home/pfaria/projects/coi/target/debug/build/proc-macro2-d1957ceb72a583e6/build-script-build"],"executable":null,"fresh":true}
{"reason":"compiler-artifact","package_id":"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["lib"],"crate_types":["lib"],"name":"unicode-xid","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/unicode-xid-0.2.0/src/lib.rs","edition":"2015","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["default"],"filenames":["/home/pfaria/projects/coi/target/debug/deps/libunicode_xid-cfcbecc30409d14b.rlib","/home/pfaria/projects/coi/target/debug/deps/libunicode_xid-cfcbecc30409d14b.rmeta"],"executable":null,"fresh":true}
{"reason":"compiler-artifact","package_id":"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["custom-build"],"crate_types":["bin"],"name":"build-script-build","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/syn-1.0.14/build.rs","edition":"2018","doctest":false},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["clone-impls","default","derive","full","parsing","printing","proc-macro","quote"],"filenames":["/home/pfaria/projects/coi/target/debug/build/syn-2cb6d520c72b2228/build-script-build"],"executable":null,"fresh":true}
{"reason":"build-script-executed","package_id":"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)","linked_libs":[],"linked_paths":[],"cfgs":["use_proc_macro","wrap_proc_macro"],"env":[],"out_dir":"/home/pfaria/projects/coi/target/debug/build/proc-macro2-80ebaef47242d8b8/out"}
{"reason":"compiler-artifact","package_id":"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["lib"],"crate_types":["lib"],"name":"proc-macro2","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/proc-macro2-1.0.8/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["default","proc-macro"],"filenames":["/home/pfaria/projects/coi/target/debug/deps/libproc_macro2-58630165de29d27e.rlib","/home/pfaria/projects/coi/target/debug/deps/libproc_macro2-58630165de29d27e.rmeta"],"executable":null,"fresh":true}
{"reason":"build-script-executed","package_id":"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)","linked_libs":[],"linked_paths":[],"cfgs":["syn_disable_nightly_tests"],"env":[],"out_dir":"/home/pfaria/projects/coi/target/debug/build/syn-67e46b59d7d47e2a/out"}
{"reason":"compiler-artifact","package_id":"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["lib"],"crate_types":["lib"],"name":"quote","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/quote-1.0.2/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["default","proc-macro"],"filenames":["/home/pfaria/projects/coi/target/debug/deps/libquote-08e6bc44c5e16ed2.rlib","/home/pfaria/projects/coi/target/debug/deps/libquote-08e6bc44c5e16ed2.rmeta"],"executable":null,"fresh":true}
{"reason":"compiler-artifact","package_id":"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["lib"],"crate_types":["lib"],"name":"syn","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/syn-1.0.14/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["clone-impls","default","derive","full","parsing","printing","proc-macro","quote"],"filenames":["/home/pfaria/projects/coi/target/debug/deps/libsyn-156e2a52d741215a.rlib","/home/pfaria/projects/coi/target/debug/deps/libsyn-156e2a52d741215a.rmeta"],"executable":null,"fresh":true}
{"reason":"compiler-artifact","package_id":"thiserror-impl 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"thiserror-impl","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/thiserror-impl-1.0.10/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":[],"filenames":["/home/pfaria/projects/coi/target/debug/deps/libthiserror_impl-98c0fd2b98e96fb5.so"],"executable":null,"fresh":true}
{"reason":"compiler-artifact","package_id":"thiserror 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)","target":{"kind":["lib"],"crate_types":["lib"],"name":"thiserror","src_path":"/home/pfaria/.cargo/registry/src/git.cakeli.workers.dev-1ecc6299db9ec823/thiserror-1.0.10/src/lib.rs","edition":"2018","doctest":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":[],"filenames":["/home/pfaria/projects/coi/target/debug/deps/libthiserror-d0c7d80784654e8f.rmeta"],"executable":null,"fresh":true}
{"reason":"compiler-message","package_id":"coi-derive 0.8.0 (path+file:///home/pfaria/projects/coi/coi-derive)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"coi-derive","src_path":"/home/pfaria/projects/coi/coi-derive/src/lib.rs","edition":"2018","doctest":true},"message":{"rendered":"error[E0412]: cannot find type `Ident` in this scope\n  --> coi-derive/src/symbol.rs:15:28\n   |\n15 | impl PartialEq<Symbol> for Ident {\n   |                            ^^^^^ not found in this scope\n   |\nhelp: possible candidates are found in other modules, you can import them into scope\n   |\n6  | use proc_macro2::Ident;\n   |\n6  | use proc_macro::Ident;\n   |\n6  | use proc_macro::bridge::server::Ident;\n   |\n6  | use quote::__rt::Ident;\n   |\n     and 1 other candidate\n\n","children":[{"children":[],"code":null,"level":"help","message":"possible candidates are found in other modules, you can import them into scope","rendered":null,"spans":[{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro2::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro::bridge::server::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use quote::__rt::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use syn::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]}]}],"code":{"code":"E0412","explanation":"The type name used is not in scope.\n\nErroneous code examples:\n\n```compile_fail,E0412\nimpl Something {} // error: type name `Something` is not in scope\n\n// or:\n\ntrait Foo {\n    fn bar(N); // error: type name `N` is not in scope\n}\n\n// or:\n\nfn foo(x: T) {} // type name `T` is not in scope\n```\n\nTo fix this error, please verify you didn't misspell the type name, you did\ndeclare it or imported it into the scope. Examples:\n\n```\nstruct Something;\n\nimpl Something {} // ok!\n\n// or:\n\ntrait Foo {\n    type N;\n\n    fn bar(_: Self::N); // ok!\n}\n\n// or:\n\nfn foo<T>(x: T) {} // ok!\n```\n\nAnother case that causes this error is when a type is imported into a parent\nmodule. To fix this, you can follow the suggestion and use File directly or\n`use super::File;` which will import the types from the parent namespace. An\nexample that causes this error is below:\n\n```compile_fail,E0412\nuse std::fs::File;\n\nmod foo {\n    fn some_function(f: File) {}\n}\n```\n\n```\nuse std::fs::File;\n\nmod foo {\n    // either\n    use super::File;\n    // or\n    // use std::fs::File;\n    fn foo(f: File) {}\n}\n# fn main() {} // don't insert it for us; that'll break imports\n```\n"},"level":"error","message":"cannot find type `Ident` in this scope","spans":[{"byte_end":432,"byte_start":427,"column_end":33,"column_start":28,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":"not found in this scope","line_end":15,"line_start":15,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":33,"highlight_start":28,"text":"impl PartialEq<Symbol> for Ident {"}]}]}}
{"reason":"compiler-message","package_id":"coi-derive 0.8.0 (path+file:///home/pfaria/projects/coi/coi-derive)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"coi-derive","src_path":"/home/pfaria/projects/coi/coi-derive/src/lib.rs","edition":"2018","doctest":true},"message":{"rendered":"error[E0412]: cannot find type `Ident` in this scope\n  --> coi-derive/src/symbol.rs:21:36\n   |\n21 | impl<'a> PartialEq<Symbol> for &'a Ident {\n   |                                    ^^^^^ not found in this scope\n   |\nhelp: possible candidates are found in other modules, you can import them into scope\n   |\n6  | use proc_macro2::Ident;\n   |\n6  | use proc_macro::Ident;\n   |\n6  | use proc_macro::bridge::server::Ident;\n   |\n6  | use quote::__rt::Ident;\n   |\n     and 1 other candidate\n\n","children":[{"children":[],"code":null,"level":"help","message":"possible candidates are found in other modules, you can import them into scope","rendered":null,"spans":[{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro2::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro::bridge::server::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use quote::__rt::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use syn::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]}]}],"code":{"code":"E0412","explanation":"The type name used is not in scope.\n\nErroneous code examples:\n\n```compile_fail,E0412\nimpl Something {} // error: type name `Something` is not in scope\n\n// or:\n\ntrait Foo {\n    fn bar(N); // error: type name `N` is not in scope\n}\n\n// or:\n\nfn foo(x: T) {} // type name `T` is not in scope\n```\n\nTo fix this error, please verify you didn't misspell the type name, you did\ndeclare it or imported it into the scope. Examples:\n\n```\nstruct Something;\n\nimpl Something {} // ok!\n\n// or:\n\ntrait Foo {\n    type N;\n\n    fn bar(_: Self::N); // ok!\n}\n\n// or:\n\nfn foo<T>(x: T) {} // ok!\n```\n\nAnother case that causes this error is when a type is imported into a parent\nmodule. To fix this, you can follow the suggestion and use File directly or\n`use super::File;` which will import the types from the parent namespace. An\nexample that causes this error is below:\n\n```compile_fail,E0412\nuse std::fs::File;\n\nmod foo {\n    fn some_function(f: File) {}\n}\n```\n\n```\nuse std::fs::File;\n\nmod foo {\n    // either\n    use super::File;\n    // or\n    // use std::fs::File;\n    fn foo(f: File) {}\n}\n# fn main() {} // don't insert it for us; that'll break imports\n```\n"},"level":"error","message":"cannot find type `Ident` in this scope","spans":[{"byte_end":547,"byte_start":542,"column_end":41,"column_start":36,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":"not found in this scope","line_end":21,"line_start":21,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":41,"highlight_start":36,"text":"impl<'a> PartialEq<Symbol> for &'a Ident {"}]}]}}
{"reason":"compiler-message","package_id":"coi-derive 0.8.0 (path+file:///home/pfaria/projects/coi/coi-derive)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"coi-derive","src_path":"/home/pfaria/projects/coi/coi-derive/src/lib.rs","edition":"2018","doctest":true},"message":{"rendered":"error[E0412]: cannot find type `Ident` in this scope\n  --> coi-derive/src/symbol.rs:46:38\n   |\n46 |     pub(crate) fn as_ident(&self) -> Ident {\n   |                                      ^^^^^ not found in this scope\n   |\nhelp: possible candidates are found in other modules, you can import them into scope\n   |\n6  | use proc_macro2::Ident;\n   |\n6  | use proc_macro::Ident;\n   |\n6  | use proc_macro::bridge::server::Ident;\n   |\n6  | use quote::__rt::Ident;\n   |\n     and 1 other candidate\n\n","children":[{"children":[],"code":null,"level":"help","message":"possible candidates are found in other modules, you can import them into scope","rendered":null,"spans":[{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro2::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use proc_macro::bridge::server::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use quote::__rt::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":113,"byte_start":113,"column_end":1,"column_start":1,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":null,"line_end":6,"line_start":6,"suggested_replacement":"use syn::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]}]}],"code":{"code":"E0412","explanation":"The type name used is not in scope.\n\nErroneous code examples:\n\n```compile_fail,E0412\nimpl Something {} // error: type name `Something` is not in scope\n\n// or:\n\ntrait Foo {\n    fn bar(N); // error: type name `N` is not in scope\n}\n\n// or:\n\nfn foo(x: T) {} // type name `T` is not in scope\n```\n\nTo fix this error, please verify you didn't misspell the type name, you did\ndeclare it or imported it into the scope. Examples:\n\n```\nstruct Something;\n\nimpl Something {} // ok!\n\n// or:\n\ntrait Foo {\n    type N;\n\n    fn bar(_: Self::N); // ok!\n}\n\n// or:\n\nfn foo<T>(x: T) {} // ok!\n```\n\nAnother case that causes this error is when a type is imported into a parent\nmodule. To fix this, you can follow the suggestion and use File directly or\n`use super::File;` which will import the types from the parent namespace. An\nexample that causes this error is below:\n\n```compile_fail,E0412\nuse std::fs::File;\n\nmod foo {\n    fn some_function(f: File) {}\n}\n```\n\n```\nuse std::fs::File;\n\nmod foo {\n    // either\n    use super::File;\n    // or\n    // use std::fs::File;\n    fn foo(f: File) {}\n}\n# fn main() {} // don't insert it for us; that'll break imports\n```\n"},"level":"error","message":"cannot find type `Ident` in this scope","spans":[{"byte_end":1080,"byte_start":1075,"column_end":43,"column_start":38,"expansion":null,"file_name":"coi-derive/src/symbol.rs","is_primary":true,"label":"not found in this scope","line_end":46,"line_start":46,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":43,"highlight_start":38,"text":"    pub(crate) fn as_ident(&self) -> Ident {"}]}]}}
{"reason":"compiler-message","package_id":"coi-derive 0.8.0 (path+file:///home/pfaria/projects/coi/coi-derive)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"coi-derive","src_path":"/home/pfaria/projects/coi/coi-derive/src/lib.rs","edition":"2018","doctest":true},"message":{"rendered":"error: aborting due to 3 previous errors\n\n","children":[],"code":null,"level":"error","message":"aborting due to 3 previous errors","spans":[]}}
{"reason":"compiler-message","package_id":"coi-derive 0.8.0 (path+file:///home/pfaria/projects/coi/coi-derive)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"coi-derive","src_path":"/home/pfaria/projects/coi/coi-derive/src/lib.rs","edition":"2018","doctest":true},"message":{"rendered":"For more information about this error, try `rustc --explain E0412`.\n","children":[],"code":null,"level":"failure-note","message":"For more information about this error, try `rustc --explain E0412`.","spans":[]}}

@Nashenas88
Copy link
Contributor Author

Here's a photo of the popup. For the life of me I couldn't figure out how to get a screen shot with that popup in Linux.

IMG_20200225_200324

@Nashenas88
Copy link
Contributor Author

Also, when hovering over the underlined Ident and selecting Quick Fix... The possible candidates ... option is the only one available. Might that help narrow down the issue?

@Nashenas88
Copy link
Contributor Author

I can try working on a minimized version of this sometime tonight, or if I don't get to it, then tomorrow morning.

@kiljacken
Copy link
Contributor

Could you share the first 10 or so lines of symbol.rs? That way I can get a better idea of whether it's rustc producing a weird line or rust-analyzer.

@lnicola
Copy link
Member

lnicola commented Feb 26, 2020

The first fix in that picture is a rustc suggestion.

@kiljacken
Copy link
Contributor

@lnicola Right, and the cargo-watch functionality provides those when they're machine applicable, but I'm not sure if we're converting the line offset wrong, or whether rustc is providing the wrong line, or something completely different :)

@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Feb 26, 2020

I can reproduce it with just

#[derive(Copy, Clone)]
pub struct Symbol(&'static str);

pub const CRATE: Symbol = Symbol("crate");
pub const PROVIDES: Symbol = Symbol("provides");
pub const WITH: Symbol = Symbol("with");

impl PartialEq<Symbol> for Ident {}

in foo.rs,

extern crate proc_macro;

mod foo;

in lib.rs

and

[package]
name = "proc-macro-test"
version = "0.1.0"

[lib]
proc-macro = true

in Cargo.toml

With the rust-analyzer version : rust-analyzer ae0aeb1 version (freshmost currently), latest stable rustc and the cargo watch turned on (that was the thing I did not enable before, thank you for mentioning it).

@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Feb 26, 2020

For this case, cargo check --message-format json produces:

{"reason":"compiler-message","package_id":"proc-macro-test 0.1.0 (path+file:///Users/someonetoignore/Downloads/tmp_dir/proc-macro-test)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"proc-macro-test","src_path":"/Users/someonetoignore/Downloads/tmp_dir/proc-macro-test/src/lib.rs","edition":"2015","doctest":true},"message":{"rendered":"error[E0412]: cannot find type `Ident` in this scope\n --> src/zzz.rs:8:28\n  |\n8 | impl PartialEq<Symbol> for Ident {}\n  |                            ^^^^^ not found in this scope\n  |\nhelp: possible candidates are found in other modules, you can import them into scope\n  |\n2 | use proc_macro::Ident;\n  |\n2 | use proc_macro::bridge::server::Ident;\n  |\n\n","children":[{"children":[],"code":null,"level":"help","message":"possible candidates are found in other modules, you can import them into scope","rendered":null,"spans":[{"byte_end":23,"byte_start":23,"column_end":1,"column_start":1,"expansion":null,"file_name":"src/zzz.rs","is_primary":true,"label":null,"line_end":2,"line_start":2,"suggested_replacement":"use proc_macro::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]},{"byte_end":23,"byte_start":23,"column_end":1,"column_start":1,"expansion":null,"file_name":"src/zzz.rs","is_primary":true,"label":null,"line_end":2,"line_start":2,"suggested_replacement":"use proc_macro::bridge::server::Ident;\n\n","suggestion_applicability":"Unspecified","text":[{"highlight_end":1,"highlight_start":1,"text":"pub struct Symbol(&'static str);"}]}]}],"code":{"code":"E0412","explanation":"The type name used is not in scope.\n\nErroneous code examples:\n\n```compile_fail,E0412\nimpl Something {} // error: type name `Something` is not in scope\n\n// or:\n\ntrait Foo {\n    fn bar(N); // error: type name `N` is not in scope\n}\n\n// or:\n\nfn foo(x: T) {} // type name `T` is not in scope\n```\n\nTo fix this error, please verify you didn't misspell the type name, you did\ndeclare it or imported it into the scope. Examples:\n\n```\nstruct Something;\n\nimpl Something {} // ok!\n\n// or:\n\ntrait Foo {\n    type N;\n\n    fn bar(_: Self::N); // ok!\n}\n\n// or:\n\nfn foo<T>(x: T) {} // ok!\n```\n\nAnother case that causes this error is when a type is imported into a parent\nmodule. To fix this, you can follow the suggestion and use File directly or\n`use super::File;` which will import the types from the parent namespace. An\nexample that causes this error is below:\n\n```compile_fail,E0412\nuse std::fs::File;\n\nmod foo {\n    fn some_function(f: File) {}\n}\n```\n\n```\nuse std::fs::File;\n\nmod foo {\n    // either\n    use super::File;\n    // or\n    // use std::fs::File;\n    fn foo(f: File) {}\n}\n# fn main() {} // don't insert it for us; that'll break imports\n```\n"},"level":"error","message":"cannot find type `Ident` in this scope","spans":[{"byte_end":223,"byte_start":218,"column_end":33,"column_start":28,"expansion":null,"file_name":"src/zzz.rs","is_primary":true,"label":"not found in this scope","line_end":8,"line_start":8,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":33,"highlight_start":28,"text":"impl PartialEq<Symbol> for Ident {}"}]}]}}
{"reason":"compiler-message","package_id":"proc-macro-test 0.1.0 (path+file:///Users/someonetoignore/Downloads/tmp_dir/proc-macro-test)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"proc-macro-test","src_path":"/Users/someonetoignore/Downloads/tmp_dir/proc-macro-test/src/lib.rs","edition":"2015","doctest":true},"message":{"rendered":"error: aborting due to previous error\n\n","children":[],"code":null,"level":"error","message":"aborting due to previous error","spans":[]}}
{"reason":"compiler-message","package_id":"proc-macro-test 0.1.0 (path+file:///Users/someonetoignore/Downloads/tmp_dir/proc-macro-test)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"proc-macro-test","src_path":"/Users/someonetoignore/Downloads/tmp_dir/proc-macro-test/src/lib.rs","edition":"2015","doctest":true},"message":{"rendered":"For more information about this error, try `rustc --explain E0412`.\n","children":[],"code":null,"level":"failure-note","message":"For more information about this error, try `rustc --explain E0412`.","spans":[]}}

The culprit is in one of the spans:

{
    "byte_end": 23,
    "byte_start": 23,
    "column_end": 1,
    "column_start": 1,
    "expansion": null,
    "file_name": "src/zzz.rs",
    "is_primary": true,
    "label": null,
    "line_end": 2,
    "line_start": 2,
    "suggested_replacement": "use proc_macro::Ident;\n\n",
    "suggestion_applicability": "Unspecified",
    "text": [
        {
            "highlight_end": 1,
            "highlight_start": 1,
            "text": "pub struct Symbol(&'static str);"
        }
    ]
},

line_end and line_start are 2, so it looks like cargo check does not consider derive attribute, not us.

Should we create an issue for cargo?

@bjorn3
Copy link
Member

bjorn3 commented Feb 26, 2020

This is likely a rustc bug, not a cargo bug.

@Nashenas88
Copy link
Contributor Author

I file a bug in the rust repo, so no need to track this here anymore.

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

No branches or pull requests

6 participants