Skip to content

Unused import warning fires incorrectly for imports used and shadowed by decl_macro's. #54476

Open
@eddyb

Description

@eddyb

I wanted to demonstrate name resolution detecting maro expansion shadowing

#![feature(decl_macro)]

mod foo {
    pub macro import_bar($name:ident) {
        use bar::time_travel as $name;
    }
}

mod bar {
    pub macro time_travel($name:ident) {
        // The uncommented version never errors.
        use foo::import_bar as $name;
        // use bar::time_travel as $name;
    }
}

use foo::*;

// Only if the invocation uses the glob import and `time_travel`
// shadows it to something *different*, does it produce an error.
/*foo::*/import_bar!(m2);
m2!(import_bar);

However, while the example works as expected, "warning: unused import: foo::*" is produced, despite the import being used by the first macro invocation (and trying to remove it results in an error).

This is likely caused by us marking imports as used after expansion is finished, so the use foo::import_bar as import_bar; import gets marked as used, instead of use foo::*;.

But the macro couldn't have expanded without the later-shadowed glob import, so maybe expansion should mark the imports used to find the macro, as used, early?

cc @petrochenkov @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-decl_macro`#![feature(decl_macro)]`L-unused_importsLint: unused_importsT-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