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

rustc --cfg parsing is lax; ICEs on --cfg "" #23301

Closed
rprichard opened this issue Mar 12, 2015 · 1 comment
Closed

rustc --cfg parsing is lax; ICEs on --cfg "" #23301

rprichard opened this issue Mar 12, 2015 · 1 comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@rprichard
Copy link
Contributor

$ RUST_BACKTRACE=1 rustc --cfg ""
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'position 0 does not resolve to a source location', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/codemap.rs:773

stack backtrace:
   1:     0x7fe3b3c72752 - sys::backtrace::write::haa06f2eb4ff4d5e5ADA
   2:     0x7fe3b3c98792 - panicking::on_panic::hdcb20beee702c0b03IJ
   3:     0x7fe3b3bded69 - rt::unwind::begin_unwind_inner::hbe159b4d97e14ee66oJ
   4:     0x7fe3b3bdf111 - rt::unwind::begin_unwind_fmt::haee4ef7e01d3657fHnJ
   5:     0x7fe3b1196a38 - codemap::CodeMap::lookup_filemap_idx::h96ac1c59c75dc900u7C
   6:     0x7fe3b1194bc1 - codemap::CodeMap::lookup_char_pos::h51279553f4627f7bSUC
   7:     0x7fe3b11950f5 - codemap::CodeMap::lookup_char_pos_adj::hcaba10a387a5285a5UC
   8:     0x7fe3b119540d - codemap::CodeMap::span_to_string::hfafeb005cfdb9b99yVC
   9:     0x7fe3b11edca2 - diagnostic::emit::h3699619c2b53fd34oCE
  10:     0x7fe3b11eb5b7 - diagnostic::EmitterWriter.Emitter::emit::h192eeee6a59af027ZyE
  11:     0x7fe3b11e9d67 - diagnostic::Handler::emit::h8baf6fed67201d3d3eE
  12:     0x7fe3b119168a - diagnostic::SpanHandler::span_fatal::h087dc0374fe394cem2D
  13:     0x7fe3b11fe744 - parse::parser::Parser<'a>::parse_ident::hdcb90b6ba49d1733g3H
  14:     0x7fe3b124ebc6 - parse::attr::Parser<'a>.ParserAttr::parse_meta_item::h7d7f52f8f6262767m9T
  15:     0x7fe3b1251b6e - parse::parse_meta_from_source_str::hbce91171fcad4c3axvU
  16:     0x7fe3b1e62ec4 - iter::Map<I, F>.Iterator::next::h4056714858292786413
  17:     0x7fe3b1e62a47 - session::config::parse_cfgspecs::h6d2932293b2e7b8azip
  18:     0x7fe3b1e65459 - session::config::build_session_options::hbe60bdcd2bec83684ip
  19:     0x7fe3b4336cdc - run_compiler::h6c6deadca3c1da44N6b
  20:     0x7fe3b43356fc - thunk::F.Invoke<A, R>::invoke::h17587398869216592240
  21:     0x7fe3b4334350 - rt::unwind::try::try_fn::h13136719022791656042
  22:     0x7fe3b3d045b8 - rust_try_inner
  23:     0x7fe3b3d045a5 - rust_try
  24:     0x7fe3b4334adb - thunk::F.Invoke<A, R>::invoke::h15245498111397843904
  25:     0x7fe3b3c85f95 - sys::thread::thread_start::hf986f8b6540d7d70daF
  26:     0x7fe3adccf181 - start_thread
  27:     0x7fe3b385747c - __clone
  28:                0x0 - <unknown>

The compiler is trying to report that the --cfg argument is invalid, but I guess it's not prepared to handle 0-length source code?

$ rustc --version --verbose
rustc 1.0.0-nightly (cfea8ec41 2015-03-10) (built 2015-03-11)
binary: rustc
commit-hash: cfea8ec41699e25c8fb524d625190f0cb860dc71
commit-date: 2015-03-10
build-date: 2015-03-11
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly

I also think it's a little strange that I can pass arbitrary source code to the --cfg command-line argument, and trailing source code is frequently ignored:

e.g. I think these two invocations are equivalent:

rustc --cfg foo,bar --cfg '/*foo*/ feature="A","B" <-THIS IS IGNORED->' hello.rs
rustc --cfg foo     --cfg 'feature="A"' hello.rs

I'm also wondering if the syntax could be just --cfg feature=A. The quoting is inconvenient.

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Mar 19, 2015
@Stebalien
Copy link
Contributor

IMHO, this is a bug in libsyntax/codemap.rs:lookup_filemap_idx (reproduced below). The comment incorrectly states that we can't possibly be looking for a length zero filemap but, in this case, we're looking for a zero-length span in a zero-length filemap. I don't know if there's any clean way to fix this because of the way filemaps work. The simple fix is to say that one must not pass empty strings to parse_*_from_source_str functions. Something like (untested):

diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs
index b999929..dc8f9fe 100644
--- a/src/librustc/session/config.rs
+++ b/src/librustc/session/config.rs
@@ -849,6 +849,9 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
 // Convert strings provided as --cfg [cfgspec] into a crate_cfg
 pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
     cfgspecs.into_iter().map(|s| {
+        if &s == "" {
+            early_error("empty --cfg argument");
+        }
         parse::parse_meta_from_source_str("cfgspec".to_string(),
                                           s.to_string(),
                                           Vec::new(),
    fn lookup_filemap_idx(&self, pos: BytePos) -> usize {
        let files = self.files.borrow();
        let files = &*files;
        let len = files.len();
        let mut a = 0;
        let mut b = len;
        while b - a > 1 {
            let m = (a + b) / 2;
            if files[m].start_pos > pos {
                b = m;
            } else {
                a = m;
            }
        }
        // There can be filemaps with length 0. These have the same start_pos as
        // the previous filemap, but are not the filemaps we want (because they
        // are length 0, they cannot contain what we are looking for). So,
        // rewind until we find a useful filemap.
        loop {
            let lines = files[a].lines.borrow();
            let lines = lines;
            if !lines.is_empty() {
                break;
            }
            if a == 0 {
                panic!("position {} does not resolve to a source location",
                      pos.to_usize());
            }
            a -= 1;
        }
        if a >= len {
            panic!("position {} does not resolve to a source location",
                  pos.to_usize())
        }

        return a;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

3 participants