Skip to content

Precedence of type macros is confusing #52307

Closed
@jendrikw

Description

@jendrikw

When calling an associated function on a struct, type macros behave very strange and produce weird error messages.

Example on playground


struct S<T>(PhantomData<T>);

impl<T> S<T> {
    fn associated_function() {}
}

macro_rules! TypeMacro {
    ($x:ty) => { S<$x> };
}

fn main() {
    // compare the error mesages:
    
    // error: expected one of `.`, `;`, `?`, or an operator, found `::`
    let _ = TypeMacro!(u8)::associated_function();
    
    // error: chained comparison operators require parentheses
    let _ = S<u8>::associated_function();
    
    // conclusion:
    // TypeMacro!(u8) is not the same S<u8>
    
    
    // this works
    let _ = <S<u8>>::associated_function();
    let _ = S::<u8>::associated_function();
    let _ = <TypeMacro!(u8)>::associated_function();
}

Naturally, one would expect that TypeMacro!(u8) is the same as S<u8> because it is defined that way.

The result is the same on all rust versions currently in playground

  • 1.27.1
  • 1.28.0-beta.9 2018-07-09 718e759
  • 1.29.0-nightly 2018-07-10 e5f6498

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an AST

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions