Skip to content

ICE when macro-generated code triggers "defined multiple times" error #57071

Closed
@dtolnay

Description

@dtolnay

As of rustc 1.33.0-nightly (2d3e909 2018-12-22) the following macro invocation fails with an ICE. Mentioning @mockersf and @estebank because this may have to do with slicing introduced in #55113.

repro/src/lib.rs

use repro_macro::m;

#[m]
struct S;

repro-macro/src/lib.rs

extern crate proc_macro;

use proc_macro::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::FromIterator;

#[proc_macro_attribute]
pub fn m(_args: TokenStream, input: TokenStream) -> TokenStream {
    let span = input.into_iter().next().unwrap().span();

    // struct S;
    // use self::S;
    TokenStream::from_iter(vec![
        TokenTree::Ident(Ident::new("struct", Span::call_site())),
        TokenTree::Ident(Ident::new("S", span)),
        TokenTree::Punct(Punct::new(';', Spacing::Alone)),
        TokenTree::Ident(Ident::new("use", Span::call_site())),
        TokenTree::Ident(Ident::new("self", Span::call_site())),
        TokenTree::Punct(Punct::new(':', Spacing::Joint)),
        TokenTree::Punct(Punct::new(':', Spacing::Alone)),
        TokenTree::Ident(Ident::new("S", span)),
        TokenTree::Punct(Punct::new(';', Spacing::Alone)),
    ])
}
thread 'rustc' panicked at 'byte index 11 is out of bounds of `#[m]`', src/libcore/str/mod.rs:2091:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:72
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:60
             at src/libstd/panicking.rs:210
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:225
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:492
   6: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:395
   7: rust_begin_unwind
             at src/libstd/panicking.rs:322
   8: core::panicking::panic_fmt
             at src/libcore/panicking.rs:95
   9: core::str::slice_error_fail
             at src/libcore/str/mod.rs:0
  10: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index::{{closure}}
  11: rustc_resolve::Resolver::report_conflict
  12: rustc_resolve::resolve_imports::ImportResolver::resolve_imports
  13: rustc_resolve::macros::<impl syntax::ext::base::Resolver for rustc_resolve::Resolver<'a>>::resolve_imports
  14: syntax::ext::expand::MacroExpander::expand_fragment
  15: syntax::ext::expand::MacroExpander::expand_crate
  16: rustc_driver::driver::phase_2_configure_and_expand_inner::{{closure}}
  17: rustc::util::common::time
  18: rustc_driver::driver::phase_2_configure_and_expand
  19: rustc_driver::driver::compile_input
  20: rustc_driver::run_compiler_with_pool
  21: <scoped_tls::ScopedKey<T>>::set
  22: rustc_driver::run_compiler
  23: <scoped_tls::ScopedKey<T>>::set
  24: syntax::with_globals
  25: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:102
  26: <F as alloc::boxed::FnBox<A>>::call_box
  27: std::sys::unix::thread::Thread::new::thread_start
             at /rustc/2d3e909e4e68259e15ca2908ff9e854f0a68bbec/src/liballoc/boxed.rs:688
             at src/libstd/sys_common/thread.rs:24
             at src/libstd/sys/unix/thread.rs:91
  28: start_thread
  29: clone
query stack during panic:
end of query stack

error: internal compiler error: unexpected panic

Same repro in script form:

#!/bin/bash

cargo new --lib repro
cargo new --lib repro-macro

echo >repro/src/lib.rs '
use repro_macro::m;

#[m]
struct S;
'

echo >>repro/Cargo.toml '
repro-macro = { path = "../repro-macro" }
'

echo >repro-macro/src/lib.rs '
extern crate proc_macro;

use proc_macro::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::FromIterator;

#[proc_macro_attribute]
pub fn m(_args: TokenStream, input: TokenStream) -> TokenStream {
    let span = input.into_iter().next().unwrap().span();

    // struct S;
    // use self::S;
    TokenStream::from_iter(vec![
        TokenTree::Ident(Ident::new("struct", Span::call_site())),
        TokenTree::Ident(Ident::new("S", span)),
        TokenTree::Punct(Punct::new(59 as char, Spacing::Alone)),
        TokenTree::Ident(Ident::new("use", Span::call_site())),
        TokenTree::Ident(Ident::new("self", Span::call_site())),
        TokenTree::Punct(Punct::new(58 as char, Spacing::Joint)),
        TokenTree::Punct(Punct::new(58 as char, Spacing::Alone)),
        TokenTree::Ident(Ident::new("S", span)),
        TokenTree::Punct(Punct::new(59 as char, Spacing::Alone)),
    ])
}
'

echo >>repro-macro/Cargo.toml '
[lib]
proc-macro = true
'

cargo check --manifest-path repro/Cargo.toml

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions