Skip to content

melt the ICE when lowering an impossible range #32267

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

Merged
merged 4 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ impl NodeIdAssigner for Session {
fn peek_node_id(&self) -> NodeId {
self.next_node_id.get().checked_add(1).unwrap()
}

fn diagnostic(&self) -> &errors::Handler {
self.diagnostic()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um. This looks... weird. Did I introduce an infinite recursion (I didn't see a warning) or it's OK because inherent fns take precedence over trait fns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, inherent has precedence.

and looking more at this has made me realized that one cannot resort to UFCS as a way to try to "clarify" that the inherent method is what is intended. There's no way to say "I want an inherent impl and only an inherent impl", at least not via UFCS...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to say "I want an inherent impl and only an inherent impl"

That seems good. Then you can factor out an inherent method into a trait method without a breaking change, is that true?

}
}

fn split_msg_into_multilines(msg: &str) -> Option<String> {
Expand Down
9 changes: 8 additions & 1 deletion src/librustc_front/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use std::collections::HashMap;
use std::iter;
use syntax::ast::*;
use syntax::attr::{ThinAttributes, ThinAttributesExt};
use syntax::errors::Handler;
use syntax::ext::mtwt;
use syntax::ptr::P;
use syntax::codemap::{respan, Spanned, Span};
Expand Down Expand Up @@ -140,6 +141,11 @@ impl<'a, 'hir> LoweringContext<'a> {
result
}
}

// Panics if this LoweringContext's NodeIdAssigner is not able to emit diagnostics.
fn diagnostic(&self) -> &Handler {
self.id_assigner.diagnostic()
}
}

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

_ => panic!("impossible range in AST"),
_ => panic!(lctx.diagnostic().span_fatal(e.span,
"inclusive range with no end"))
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::PathParameters::*;
use attr::ThinAttributes;
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
use abi::Abi;
use errors;
use ext::base;
use ext::tt::macro_parser;
use parse::token::InternedString;
Expand Down Expand Up @@ -344,6 +345,10 @@ pub const DUMMY_NODE_ID: NodeId = !0;
pub trait NodeIdAssigner {
fn next_node_id(&self) -> NodeId;
fn peek_node_id(&self) -> NodeId;

fn diagnostic(&self) -> &errors::Handler {
panic!("this ID assigner cannot emit diagnostics")
}
}

/// The AST represents all type param bounds as types.
Expand Down
68 changes: 39 additions & 29 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,8 +2072,15 @@ impl<'a> Parser<'a> {
start: Option<P<Expr>>,
end: Option<P<Expr>>,
limits: RangeLimits)
-> ast::ExprKind {
ExprKind::Range(start, end, limits)
-> PResult<'a, ast::ExprKind> {
if end.is_none() && limits == RangeLimits::Closed {
Err(self.span_fatal_help(self.span,
"inclusive range with no end",
"inclusive ranges must be bounded at the end \
(`...b` or `a...b`)"))
} else {
Ok(ExprKind::Range(start, end, limits))
}
}

pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::ExprKind {
Expand Down Expand Up @@ -2961,12 +2968,12 @@ impl<'a> Parser<'a> {
lhs = self.mk_expr(lhs_span.lo, rhs.span.hi,
ExprKind::Type(lhs, rhs), None);
continue
} else if op == AssocOp::DotDot {
// If we didn’t have to handle `x..`, it would be pretty easy to generalise
// it to the Fixity::None code.
} else if op == AssocOp::DotDot || op == AssocOp::DotDotDot {
// If we didn’t have to handle `x..`/`x...`, it would be pretty easy to
// generalise it to the Fixity::None code.
//
// We have 2 alternatives here: `x..y` and `x..` The other two variants are
// handled with `parse_prefix_range_expr` call above.
// We have 2 alternatives here: `x..y`/`x...y` and `x..`/`x...` The other
// two variants are handled with `parse_prefix_range_expr` call above.
let rhs = if self.is_at_start_of_range_notation_rhs() {
let rhs = self.parse_assoc_expr_with(op.precedence() + 1,
LhsExpr::NotYetParsed);
Expand All @@ -2985,7 +2992,13 @@ impl<'a> Parser<'a> {
} else {
cur_op_span
});
let r = self.mk_range(Some(lhs), rhs, RangeLimits::HalfOpen);
let limits = if op == AssocOp::DotDot {
RangeLimits::HalfOpen
} else {
RangeLimits::Closed
};

let r = try!(self.mk_range(Some(lhs), rhs, limits));
lhs = self.mk_expr(lhs_span.lo, rhs_span.hi, r, None);
break
}
Expand All @@ -3003,8 +3016,8 @@ impl<'a> Parser<'a> {
this.parse_assoc_expr_with(op.precedence() + 1,
LhsExpr::NotYetParsed)
}),
// the only operator handled here is `...` (the other non-associative operators are
// special-cased above)
// We currently have no non-associative operators that are not handled above by
// the special cases. The code is here only for future convenience.
Fixity::None => self.with_res(
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
|this| {
Expand Down Expand Up @@ -3045,13 +3058,8 @@ impl<'a> Parser<'a> {
let aopexpr = self.mk_assign_op(codemap::respan(cur_op_span, aop), lhs, rhs);
self.mk_expr(lhs_span.lo, rhs_span.hi, aopexpr, None)
}
AssocOp::DotDotDot => {
let (lhs_span, rhs_span) = (lhs.span, rhs.span);
let r = self.mk_range(Some(lhs), Some(rhs), RangeLimits::Closed);
self.mk_expr(lhs_span.lo, rhs_span.hi, r, None)
}
AssocOp::As | AssocOp::Colon | AssocOp::DotDot => {
self.bug("As, Colon or DotDot branch reached")
AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotDot => {
self.bug("As, Colon, DotDot or DotDotDot branch reached")
}
};

Expand Down Expand Up @@ -3095,21 +3103,23 @@ impl<'a> Parser<'a> {
// RHS must be parsed with more associativity than the dots.
let next_prec = AssocOp::from_token(&tok).unwrap().precedence() + 1;
Some(self.parse_assoc_expr_with(next_prec,
LhsExpr::NotYetParsed)
.map(|x|{
hi = x.span.hi;
x
})?)
LhsExpr::NotYetParsed)
.map(|x|{
hi = x.span.hi;
x
})?)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record, I think this is much harder to read than the old version with try!. You have to parse the entire expression backwards from ? to see what it applies to.

} else {
None
};
let r = self.mk_range(None,
opt_end,
if tok == token::DotDot {
RangeLimits::HalfOpen
} else {
RangeLimits::Closed
});
let limits = if tok == token::DotDot {
RangeLimits::HalfOpen
} else {
RangeLimits::Closed
};

let r = try!(self.mk_range(None,
opt_end,
limits));
Ok(self.mk_expr(lo, hi, r, attrs))
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/compile-fail/impossible_range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Make sure that invalid ranges generate an error during HIR lowering, not an ICE

#![feature(inclusive_range_syntax)]

pub fn main() {
..;
0..;
..1;
0..1;

...; //~ERROR inclusive range with no end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused. We get to this error during lowering, so I assumed the one on the next line (which is a parse error) was recovered. But what valid AST could be left behind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A range with neither start nor end - the AST doesn't have to be well-formed (for some definition), it just must be constructable. We don't get the lowering error there because the first error was fatal. I suspect if you have a test case with just that line, you would get both a parsing and lowering error.

//~^HELP bounded at the end
0...; //~ERROR inclusive range with no end
//~^HELP bounded at the end
...1;
0...1;
}


5 changes: 3 additions & 2 deletions src/test/parse-fail/range_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(inclusive_range_syntax, inclusive_range)]

pub fn main() {
for _ in 1... {}
} //~ ERROR expected one of
for _ in 1... {} //~ERROR inclusive range with no end
//~^HELP bounded at the end
}