Skip to content

2021 prelude collision lint expands macros when used as an expression #88347

Closed
@ehuss

Description

@ehuss

The rust_2021_prelude_collisions lint suggestion seems to expand the expression for a macro, but I don't think it should. The following:

#![warn(rust_2021_prelude_collisions)]

fn bar() {}

macro_rules! foo {
    () => {{
        $crate::bar();
        S
    }};
}

trait MyTry<T> {
    fn try_into(self) -> Result<T, ()>;
}

struct S;

impl MyTry<i32> for S {
    fn try_into(self) -> Result<i32, ()> {
        unimplemented!();
    }
}

fn main() {
    foo!().try_into().unwrap();
}

The suggestion recommends replacing it with:

    MyTry::try_into({
        $crate::bar();
        S
    }).unwrap();

which will not compile. I would expect it to suggest:

MyTry::try_into(foo!()).unwrap();

Found during the crater run for:

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (b03ccace5 2021-08-24)
binary: rustc
commit-hash: b03ccace573bb91e27625c190a0f7807045a1012
commit-date: 2021-08-24
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

Labels

A-edition-2021Area: The 2021 editionA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions