Skip to content

Commit 9799cac

Browse files
committed
fatal error instead of ICE for impossible range during HIR lowering
End-less ranges (`a...`) don't parse but bad syntax extensions could conceivably produce them. Unbounded ranges (`...`) do parse and are caught here. The other panics in HIR lowering are all for unexpanded macros, which cannot be constructed by bad syntax extensions.
1 parent cd80c1b commit 9799cac

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/librustc/session/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ impl NodeIdAssigner for Session {
336336
fn peek_node_id(&self) -> NodeId {
337337
self.next_node_id.get().checked_add(1).unwrap()
338338
}
339+
340+
fn diagnostic(&self) -> &errors::Handler {
341+
self.diagnostic()
342+
}
339343
}
340344

341345
fn split_msg_into_multilines(msg: &str) -> Option<String> {

src/librustc_front/lowering.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use std::collections::HashMap;
6868
use std::iter;
6969
use syntax::ast::*;
7070
use syntax::attr::{ThinAttributes, ThinAttributesExt};
71+
use syntax::errors::Handler;
7172
use syntax::ext::mtwt;
7273
use syntax::ptr::P;
7374
use syntax::codemap::{respan, Spanned, Span};
@@ -140,6 +141,11 @@ impl<'a, 'hir> LoweringContext<'a> {
140141
result
141142
}
142143
}
144+
145+
// panics if this LoweringContext's NodeIdAssigner is not a Session
146+
fn diagnostic(&self) -> &Handler {
147+
self.id_assigner.diagnostic()
148+
}
143149
}
144150

145151
// Utility fn for setting and unsetting the cached id.
@@ -1289,7 +1295,8 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12891295
make_struct(lctx, e, &["RangeInclusive", "NonEmpty"],
12901296
&[("start", e1), ("end", e2)]),
12911297

1292-
_ => panic!("impossible range in AST"),
1298+
_ => panic!(lctx.diagnostic().span_fatal(e.span,
1299+
"inclusive range with no end"))
12931300
}
12941301
});
12951302
}

src/libsyntax/ast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use self::PathParameters::*;
1919
use attr::ThinAttributes;
2020
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
2121
use abi::Abi;
22+
use errors;
2223
use ext::base;
2324
use ext::tt::macro_parser;
2425
use parse::token::InternedString;
@@ -344,6 +345,10 @@ pub const DUMMY_NODE_ID: NodeId = !0;
344345
pub trait NodeIdAssigner {
345346
fn next_node_id(&self) -> NodeId;
346347
fn peek_node_id(&self) -> NodeId;
348+
349+
fn diagnostic(&self) -> &errors::Handler {
350+
panic!("this ID assigner cannot emit diagnostics")
351+
}
347352
}
348353

349354
/// The AST represents all type param bounds as types.

src/test/parse-fail/range_inclusive.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(inclusive_range_syntax, inclusive_range)]
1414

1515
pub fn main() {
16-
for _ in 1... {}
17-
} //~ ERROR expected one of
16+
for _ in 1... {} //~ERROR inclusive range with no end
17+
//~^HELP 28237
18+
}
1819

0 commit comments

Comments
 (0)