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

non-hygienic lifetime-shadowing check can make macros non-modular #25345

Open
pnkfelix opened this issue May 12, 2015 · 4 comments
Open

non-hygienic lifetime-shadowing check can make macros non-modular #25345

pnkfelix opened this issue May 12, 2015 · 4 comments
Labels
A-hygiene Area: Macro hygiene A-lifetimes Area: Lifetimes / regions A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

non-hygienic lifetime-shadowing check can make macros non-modular

This is sort of related to #24278

Here is some example code (playpen)

macro_rules! foo {
    ($m:ident) => {
        fn $m<'a>(x: &'a i8) -> i8 { *x }
    }
}

struct Foo<'a>(&'a i8);

impl<'a> Foo<'a> {
    foo!(bar);
    fn baz() -> i8 {
        let x = 3_i8;
        Foo::bar(&x)
    }
}

fn main() {
    println!("{}", Foo::baz());
}

This yields the error:

<anon>:3:15: 3:17 error: lifetime name `'a` shadows a lifetime name that is already in scope
<anon>:3         fn $m<'a>(x: &'a i8) -> i8 { *x }

The problem is that there is no way for the macro author to make their macro robust against all possible contexts here. In other words, the referential transparency component of hygiene demands that the lifetimes introduced by an expansion be treated as fresh, but they are not.

(Of course, there are plenty of other ways that our macro system is not hygienic, so this is likely to be filed away as a known weakness in macro_rules!. I just wanted to have a place to reference this problem.)

@steveklabnik steveklabnik added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label May 13, 2015
@steveklabnik
Copy link
Member

Triage: no change

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: still no change. I'm not sure that this is going to ever be fixed.

@jonas-schievink jonas-schievink added the A-lifetimes Area: Lifetimes / regions label Aug 9, 2021
@Aaron1011
Copy link
Member

At this point, I think we'd want an RFC before changing this.

@Aaron1011
Copy link
Member

Note that while lifetime resolution strips away the 'mixed-site' hygiene generated by macro_rules!, it does not remove the 'def-site' hygiene created by macros 2.0. As a result, the following code compiles:

#![feature(decl_macro)]

macro foo {
    () => {
        fn dummy<'a>(x: &'a i8) -> i8 { *x }
    }
}

struct Foo<'a>(&'a i8);

impl<'a> Foo<'a> {
    foo!();
    fn baz() {
        let x = 3_i8;
    }
}

fn main() {}

The 'a lifetime created by macro foo has opaque (def-site) hygiene, which does not conflict with the 'a lifetime declared in impl<'a>

@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-hygiene Area: Macro hygiene labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-hygiene Area: Macro hygiene A-lifetimes Area: Lifetimes / regions A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. 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

6 participants