Skip to content

with #![no_std] enabled, 'panic!("{{}}")' and 'panic!("{{}}",)' are different #48042

Closed

Description

@ExpHP

Discovered while trying to implement fixes for #46241. Possible back-compat hazard for trailing comma support, but hopefully not.

libstd/macros.rs

macro_rules! panic {
    () => (/* default message */);
    ($msg:expr) => (/* use msg as literal */);
    ($fmt:expr, $($arg:tt)+) => (/* use format_args! */);
}

libcore/macros.rs

macro_rules! panic {
    () => (/* default message */);
    ($msg:expr) => (/* use msg as literal */);
    ($fmt:expr, $($arg:tt)*) => (/* use format_args! */);
}

Can you spot the difference?


The definition in std fails to support trailing commas after a single string literal:

fn main() {
    panic!("a");
    panic!("a",); //~ ERROR unexpected end of macro invocation
    
    panic!("{}");
    panic!("{}",); //~ ERROR unexpected end of macro invocation
}

The definition in core accepts a trailing comma, but follows a completely different code-path with potentially different semantics.

#![no_std]

fn main() {
    panic!("a");
    panic!("a",);
    
    panic!("{}");
    panic!("{}",); //~ ERROR 1 positional argument in format string, but no arguments were given
}

Fortuitously, it seems to me that it libcore's definition produces equivalent behavior for panic!(<literal>,) and panic!(<literal>) in all cases where both forms successfully compile, meaning it should hopefully be safe to change panic!(<literal>,) to behave like panic!(<literal>).

Activity

  1. ExpHP commented on Feb 7, 2018

    @ExpHP
    ContributorAuthor

    Crap. Found an example of the sort of back-compat hazard I was talking about.

    In today's rust, the following two programs are different:

    #![no_std]
    
    fn main() {
        panic!("{{}}");  // panics with "{{}}"
    }
    #![no_std]
    
    fn main() {
        panic!("{{}}",);  // panics with "{}"
    }
  2. changed the title [-]with #![no_std] enabled, 'panic!(<literal>,)' compiles and is not necessarily the same as 'panic!(<literal>)'[/-] [+]with #![no_std] enabled, 'panic!(<literal>,)' compiles and is different from 'panic!(<literal>)'[/+] on Feb 7, 2018
  3. changed the title [-]with #![no_std] enabled, 'panic!(<literal>,)' compiles and is different from 'panic!(<literal>)'[/-] [+]with #![no_std] enabled, 'panic!("{{}}")' and 'panic!("{{}}",)' are different[/+] on Feb 7, 2018
  4. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-libs-api[DEPRECATED; DO NOT USE]
    C-bugCategory: This is a bug.
    on Feb 7, 2018
  5. added a commit that references this issue on Feb 24, 2018
    8240747
  6. added a commit that references this issue on Feb 28, 2018
    ddab91a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions