Skip to content

Commit dfb32af

Browse files
committed
Rollup merge of rust-lang#50421 - kennytm:fix-50415-ice-when-returning-range-inclusive-from-closure, r=michaelwoerister
Fix ICE when using a..=b in a closure. Fix rust-lang#50415.
2 parents 5976e8a + 83c4505 commit dfb32af

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

src/librustc/hir/lowering.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3121,9 +3121,9 @@ impl<'a> LoweringContext<'a> {
31213121
}
31223122
// Desugar `<start>..=<end>` to `std::ops::RangeInclusive::new(<start>, <end>)`
31233123
ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
3124-
// FIXME: Use head_sp directly after RangeInclusive::new() is stabilized in stage0.
3124+
// FIXME: Use e.span directly after RangeInclusive::new() is stabilized in stage0.
31253125
let span = self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
3126-
let id = self.lower_node_id(e.id);
3126+
let id = self.next_id();
31273127
let e1 = self.lower_expr(e1);
31283128
let e2 = self.lower_expr(e2);
31293129
let ty_path = P(self.std_path(span, &["ops", "RangeInclusive"], false));

src/libsyntax/attr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,7 @@ impl LitKind {
12521252

12531253
match *self {
12541254
LitKind::Str(string, ast::StrStyle::Cooked) => {
1255-
let mut escaped = String::new();
1256-
for ch in string.as_str().chars() {
1257-
escaped.extend(ch.escape_unicode());
1258-
}
1255+
let escaped = string.as_str().escape_default();
12591256
Token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None)
12601257
}
12611258
LitKind::Str(string, ast::StrStyle::Raw(n)) => {

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(non_exhaustive)]
2626
#![feature(const_atomic_usize_new)]
2727
#![feature(rustc_attrs)]
28+
#![feature(str_escape)]
2829

2930
#![recursion_limit="256"]
3031

src/libsyntax/parse/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,10 @@ pub fn char_lit(lit: &str, diag: Option<(Span, &Handler)>) -> (char, isize) {
298298
}
299299
}
300300

301-
pub fn escape_default(s: &str) -> String {
302-
s.chars().map(char::escape_default).flat_map(|x| x).collect()
303-
}
304-
305301
/// Parse a string representing a string literal into its final form. Does
306302
/// unescaping.
307303
pub fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String {
308-
debug!("parse_str_lit: given {}", escape_default(lit));
304+
debug!("str_lit: given {}", lit.escape_default());
309305
let mut res = String::with_capacity(lit.len());
310306

311307
let error = |i| format!("lexer should have rejected {} at {}", lit, i);
@@ -374,7 +370,7 @@ pub fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String {
374370
/// Parse a string representing a raw string literal into its final form. The
375371
/// only operation this does is convert embedded CRLF into a single LF.
376372
pub fn raw_str_lit(lit: &str) -> String {
377-
debug!("raw_str_lit: given {}", escape_default(lit));
373+
debug!("raw_str_lit: given {}", lit.escape_default());
378374
let mut res = String::with_capacity(lit.len());
379375

380376
let mut chars = lit.chars().peekable();

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ pub trait PrintState<'a> {
656656
style: ast::StrStyle) -> io::Result<()> {
657657
let st = match style {
658658
ast::StrStyle::Cooked => {
659-
(format!("\"{}\"", parse::escape_default(st)))
659+
(format!("\"{}\"", st.escape_default()))
660660
}
661661
ast::StrStyle::Raw(n) => {
662662
(format!("r{delim}\"{string}\"{delim}",

src/test/run-pass/issue-50415.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 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+
fn main() {
12+
// -------- Simplified test case --------
13+
14+
let _ = || 0..=1;
15+
16+
// -------- Original test case --------
17+
18+
let full_length = 1024;
19+
let range = {
20+
// do some stuff, omit here
21+
None
22+
};
23+
24+
let range = range.map(|(s, t)| s..=t).unwrap_or(0..=(full_length-1));
25+
26+
assert_eq!(range, 0..=1023);
27+
}

0 commit comments

Comments
 (0)