Coder Social home page Coder Social logo

Comments (7)

fmease avatar fmease commented on May 30, 2024 2

A more minimal repo would be:

Even more minimal:

macro_rules! cycle {
    ($expr:expr) => { const X: () = $expr; }
}

fn main() { cycle!(X); } //~ ERROR cycle detected

macro_rules!-macros aka decl macros 1.2 are only partially hygienic because only local variables follow macro hygiene but not items. When you define constant X via a 1.2 macro, you can access it from other syntax contexts. That's what happens in the example above. Another example would be

macro_rules! def { () => { const X: () = (); } }

fn main() {
    def!();
    let _ = X; // successfully resolves `X` to the const item
}

Decl macros 2.0 (#![feature(decl_macro)]) on the other hand are fully hygienic: Both locals and items are hygienic. For example, the following code succeeds compilation:

#![feature(decl_macro)]

macro cycle {
    ($expr:expr) => { const X: () = $expr; }
}

fn main() {
    const X: () = ();
    cycle!(X);
}

from rust.

fmease avatar fmease commented on May 30, 2024 1

TL;DR: Your example from the issue description passes compilation if you use decl macros 2.0 instead of 1.2 (which requires nightly): Playground.

from rust.

ChrisDenton avatar ChrisDenton commented on May 30, 2024

A more minimal repo would be:

macro_rules! cycle {
    ($expr:expr) => {{
        const UTF8: &str = $expr;
    }}
}
#[allow(unused)]
fn main() {
    const UTF8: &str = "hello";
    cycle!(UTF8);
}

from rust.

Urgau avatar Urgau commented on May 30, 2024

Not sure if it's relevant to this issue but the same error happens without the macro_rules!:

#[allow(dead_code)]
fn main() {
    const UTF8: &str = "hello";
    {
        const UTF8: &str = UTF8;
    }
}

from rust.

fmease avatar fmease commented on May 30, 2024

This works as intended. [typing explainer...]

from rust.

fmease avatar fmease commented on May 30, 2024

Closing as works as intended :)

from rust.

workingjubilee avatar workingjubilee commented on May 30, 2024

I had a feeling this might get a "working as intended" but I'm glad to hear it gets fixed by macros 2.0!

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.