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

Import suggestion breaks #[cfg] attribute for previous imports #115618

Closed
marcospb19 opened this issue Sep 6, 2023 · 0 comments · Fixed by #115630
Closed

Import suggestion breaks #[cfg] attribute for previous imports #115618

marcospb19 opened this issue Sep 6, 2023 · 0 comments · Fixed by #115630
Labels
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.

Comments

@marcospb19
Copy link
Contributor

Code

#[cfg(unix)]
use std::os::unix::fs;

fn main() {
    let _ = stdout();

    #[cfg(unix)]
    {
        // Using unix specific stuff here.
        let _function = fs::chroot::<&str>;
    }
}

Current output

error[E0425]: cannot find function `stdout` in this scope
 --> src/main.rs:5:13
  |
5 |     let _ = stdout();
  |             ^^^^^^ not found in this scope
  |
help: consider importing this function
  |
2 + use std::io::stdout;
  |

Desired output

...
help: consider importing this function
  |
3 + use std::io::stdout;
  |

((( with `3` instead of `2` )))

Rationale and extra context

After applying the suggestion, you end up with this code:

#[cfg(unix)]
use std::io::stdout;
use std::os::unix::fs;

fn main() {
    let _ = stdout();

    #[cfg(unix)]
    {
        // Using unix specific stuff here.
        let _function = fs::chroot::<&str>;
    }
}

Note that the cfg(unix) attribute is at the wrong place, so code won't compile on Windows anymore.

I'd say the ideal solution is to either:

  • Suggest import after the last import line.
  • Suggest import following some other rule where it's guaranteed to not fall under an attribute.

Other cases

No response

Anything else?

Comes from rust-lang/rust-enhanced#516 (Sublime Text's "extension" for Rust).

The extension relies on rustc's import suggestions to apply fixes (user is prompted), accepting the fix breaks your code.

Useful label: D-invalid-suggestion

@marcospb19 marcospb19 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 6, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 6, 2023
@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 7, 2023
@bors bors closed this as completed in b33ac52 Sep 9, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 9, 2023
Rollup merge of rust-lang#115630 - compiler-errors:dont-suggest-use-btw-use-and-attr, r=wesleywiser

Dont suggest use between `use` and cfg attr

Fixes rust-lang#115618
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`. 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

Successfully merging a pull request may close this issue.

3 participants