Skip to content

Commit f4e0ce6

Browse files
committed
Auto merge of #23489 - michaelwoerister:span-artihmetic-overflow-bug, r=alexcrichton
This should solve issues #23115, #23469, and #23407. As the title says, this is just a workaround. The underlying problem is that macro expansion can produce invalid spans. I've opened issue #23480 so we don't forget about that.
2 parents 7f53b94 + 1ea971f commit f4e0ce6

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/librustc/middle/astencode.rs

+15
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,27 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> {
235235
pub fn tr_span(&self, span: Span) -> Span {
236236
let imported_filemaps = &self.cdata.codemap_import_info[..];
237237

238+
let span = if span.lo > span.hi {
239+
// Currently macro expansion sometimes produces invalid Span values
240+
// where lo > hi. In order not to crash the compiler when trying to
241+
// translate these values, let's transform them into something we
242+
// can handle (and which will produce useful debug locations at
243+
// least some of the time).
244+
// This workaround is only necessary as long as macro expansion is
245+
// not fixed. FIXME(#23480)
246+
codemap::mk_sp(span.lo, span.lo)
247+
} else {
248+
span
249+
};
250+
238251
let filemap_index = {
239252
// Optimize for the case that most spans within a translated item
240253
// originate from the same filemap.
241254
let last_filemap_index = self.last_filemap_index.get();
242255

243256
if span.lo >= imported_filemaps[last_filemap_index].original_start_pos &&
257+
span.lo <= imported_filemaps[last_filemap_index].original_end_pos &&
258+
span.hi >= imported_filemaps[last_filemap_index].original_start_pos &&
244259
span.hi <= imported_filemaps[last_filemap_index].original_end_pos {
245260
last_filemap_index
246261
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
// no-prefer-dynamic
13+
14+
// compile-flags: -g
15+
16+
#[macro_use]
17+
mod crate_with_invalid_spans_macros;
18+
19+
pub fn exported_generic<T>(x: T, y: u32) -> (T, u32) {
20+
// Using the add1 macro will produce an invalid span, because the `y` passed
21+
// to the macro will have a span from this file, but the rest of the code
22+
// generated from the macro will have spans from the macro-defining file.
23+
// The AST node for the (1 + y) expression generated by the macro will then
24+
// take it's `lo` span bound from the `1` literal in the macro-defining file
25+
// and it's `hi` bound from `y` in this file, which should be lower than the
26+
// `lo` and even lower than the lower bound of the FileMap it is supposedly
27+
// contained in because the FileMap for this file was allocated earlier than
28+
// the FileMap of the macro-defining file.
29+
return (x, add1!(y));
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! add1 {
12+
($e:expr) => ({
13+
let a = 1 + $e;
14+
let b = $e + 1;
15+
a + b - 1
16+
})
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:crate_with_invalid_spans.rs
12+
13+
extern crate crate_with_invalid_spans;
14+
15+
fn main() {
16+
// The AST of `exported_generic` stored in crate_with_invalid_spans's
17+
// metadata should contain an invalid span where span.lo > span.hi.
18+
// Let's make sure the compiler doesn't crash when encountering this.
19+
let _ = crate_with_invalid_spans::exported_generic(32u32, 7u32);
20+
}

0 commit comments

Comments
 (0)