Skip to content

Import suggestion at incorrect line when first line is a struct attribute #69733

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

Open
Nashenas88 opened this issue Mar 5, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nashenas88
Copy link
Contributor

I tried this code:

(From rust-lang/rust-analyzer#3237 (comment))

#[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

And ran cargo check --message-format=json

I expected to see this happen:

A suggestion to place the imports on line 1, e.g. "line_start": 1, "line_end": 1, "suggested_replacement": "use proc_macro::Ident;"

Instead, this happened:

{"reason":"compiler-message","package_id":"proc-macro-test 0.1.0 (path+file:///home/pfaria/projects/rustc-suggestion-bug)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"proc-macro-test","src_path":"/home/pfaria/projects/rustc-suggestion-bug/src/lib.rs","edition":"2015","doctest":true},"message":{"rendered":"error[E0412]: cannot find type `Ident` in this scope\n --> src/foo.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/foo.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/foo.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/foo.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:///home/pfaria/projects/rustc-suggestion-bug)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"proc-macro-test","src_path":"/home/pfaria/projects/rustc-suggestion-bug/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:///home/pfaria/projects/rustc-suggestion-bug)","target":{"kind":["proc-macro"],"crate_types":["proc-macro"],"name":"proc-macro-test","src_path":"/home/pfaria/projects/rustc-suggestion-bug/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":[]}}

One of the above in a more readable format:

{
    "byte_end": 23,
    "byte_start": 23,
    "column_end": 1,
    "column_start": 1,
    "expansion": null,
    "file_name": "src/foo.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);"
        }
    ]
}

Note that line_start and line_end are 2, so the use statement is inserted in between the derive attribute and the struct Symbol, which causes a compilation error.

Meta

rustc --version --verbose:

rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: x86_64-unknown-linux-gnu
release: 1.41.1
LLVM version: 9.0
@Nashenas88 Nashenas88 added the C-bug Category: This is a bug. label Mar 5, 2020
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 6, 2020
@euclio
Copy link
Contributor

euclio commented May 15, 2020

Likely caused by #45216.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants