Skip to content

Commit d9769ec

Browse files
committed
Parse fully-qualified associated types in generics without whitespace
This breaks code that looks like this: let x = foo as bar << 13; Change such code to look like this: let x = (foo as bar) << 13; Closes #17362. [breaking-change]
1 parent 7e11b22 commit d9769ec

File tree

10 files changed

+53
-59
lines changed

10 files changed

+53
-59
lines changed

src/libcollections/bit.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,17 @@ impl Bitv {
314314

315315
for i in range(0, complete_words) {
316316
bitv.storage.push(
317-
(reverse_bits(bytes[i * 4 + 0]) as u32 << 0) |
318-
(reverse_bits(bytes[i * 4 + 1]) as u32 << 8) |
319-
(reverse_bits(bytes[i * 4 + 2]) as u32 << 16) |
320-
(reverse_bits(bytes[i * 4 + 3]) as u32 << 24)
317+
((reverse_bits(bytes[i * 4 + 0]) as u32) << 0) |
318+
((reverse_bits(bytes[i * 4 + 1]) as u32) << 8) |
319+
((reverse_bits(bytes[i * 4 + 2]) as u32) << 16) |
320+
((reverse_bits(bytes[i * 4 + 3]) as u32) << 24)
321321
);
322322
}
323323

324324
if extra_bytes > 0 {
325325
let mut last_word = 0u32;
326326
for (i, &byte) in bytes[complete_words*4..].iter().enumerate() {
327-
last_word |= reverse_bits(byte) as u32 << (i * 8);
327+
last_word |= (reverse_bits(byte) as u32) << (i * 8);
328328
}
329329
bitv.storage.push(last_word);
330330
}
@@ -645,7 +645,7 @@ impl Bitv {
645645
if offset >= bitv.nbits {
646646
0
647647
} else {
648-
bitv[offset] as u8 << (7 - bit)
648+
(bitv[offset] as u8) << (7 - bit)
649649
}
650650
}
651651

src/libcore/hash/sip.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ pub struct SipState {
5050
macro_rules! u8to64_le {
5151
($buf:expr, $i:expr) =>
5252
($buf[0+$i] as u64 |
53-
$buf[1+$i] as u64 << 8 |
54-
$buf[2+$i] as u64 << 16 |
55-
$buf[3+$i] as u64 << 24 |
56-
$buf[4+$i] as u64 << 32 |
57-
$buf[5+$i] as u64 << 40 |
58-
$buf[6+$i] as u64 << 48 |
59-
$buf[7+$i] as u64 << 56);
53+
($buf[1+$i] as u64) << 8 |
54+
($buf[2+$i] as u64) << 16 |
55+
($buf[3+$i] as u64) << 24 |
56+
($buf[4+$i] as u64) << 32 |
57+
($buf[5+$i] as u64) << 40 |
58+
($buf[6+$i] as u64) << 48 |
59+
($buf[7+$i] as u64) << 56);
6060
($buf:expr, $i:expr, $len:expr) =>
6161
({
6262
let mut t = 0;
6363
let mut out = 0u64;
6464
while t < $len {
65-
out |= $buf[t+$i] as u64 << t*8;
65+
out |= ($buf[t+$i] as u64) << t*8;
6666
t += 1;
6767
}
6868
out

src/librand/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub trait Rng {
7474
/// these two methods. Similarly to `next_u32`, this rarely needs
7575
/// to be called directly, prefer `r.gen()` to `r.next_u64()`.
7676
fn next_u64(&mut self) -> u64 {
77-
(self.next_u32() as u64 << 32) | (self.next_u32() as u64)
77+
((self.next_u32() as u64) << 32) | (self.next_u32() as u64)
7878
}
7979

8080
/// Return the next random f32 selected from the half-open

src/libstd/io/net/ip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ impl<'a> Parser<'a> {
267267
});
268268
match ipv4 {
269269
Some(Ipv4Addr(a, b, c, d)) => {
270-
groups[i + 0] = (a as u16 << 8) | (b as u16);
271-
groups[i + 1] = (c as u16 << 8) | (d as u16);
270+
groups[i + 0] = ((a as u16) << 8) | (b as u16);
271+
groups[i + 1] = ((c as u16) << 8) | (d as u16);
272272
return (i + 2, true);
273273
}
274274
_ => {}

src/libstd/sys/common/net.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ pub enum InAddr {
5555
pub fn ip_to_inaddr(ip: IpAddr) -> InAddr {
5656
match ip {
5757
Ipv4Addr(a, b, c, d) => {
58-
let ip = (a as u32 << 24) |
59-
(b as u32 << 16) |
60-
(c as u32 << 8) |
61-
(d as u32 << 0);
58+
let ip = ((a as u32) << 24) |
59+
((b as u32) << 16) |
60+
((c as u32) << 8) |
61+
((d as u32) << 0);
6262
In4Addr(libc::in_addr {
6363
s_addr: Int::from_be(ip)
6464
})

src/libsyntax/parse/parser.rs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -669,45 +669,22 @@ impl<'a> Parser<'a> {
669669
/// `<` and continue. If a `<` is not seen, return false.
670670
///
671671
/// This is meant to be used when parsing generics on a path to get the
672-
/// starting token. The `force` parameter is used to forcefully break up a
673-
/// `<<` token. If `force` is false, then `<<` is only broken when a lifetime
674-
/// shows up next. For example, consider the expression:
675-
///
676-
/// foo as bar << test
677-
///
678-
/// The parser needs to know if `bar <<` is the start of a generic path or if
679-
/// it's a left-shift token. If `test` were a lifetime, then it's impossible
680-
/// for the token to be a left-shift, but if it's not a lifetime, then it's
681-
/// considered a left-shift.
682-
///
683-
/// The reason for this is that the only current ambiguity with `<<` is when
684-
/// parsing closure types:
685-
///
686-
/// foo::<<'a> ||>();
687-
/// impl Foo<<'a> ||>() { ... }
688-
fn eat_lt(&mut self, force: bool) -> bool {
672+
/// starting token.
673+
fn eat_lt(&mut self) -> bool {
689674
match self.token {
690675
token::Lt => { self.bump(); true }
691676
token::BinOp(token::Shl) => {
692-
let next_lifetime = self.look_ahead(1, |t| match *t {
693-
token::Lifetime(..) => true,
694-
_ => false,
695-
});
696-
if force || next_lifetime {
697-
let span = self.span;
698-
let lo = span.lo + BytePos(1);
699-
self.replace_token(token::Lt, lo, span.hi);
700-
true
701-
} else {
702-
false
703-
}
677+
let span = self.span;
678+
let lo = span.lo + BytePos(1);
679+
self.replace_token(token::Lt, lo, span.hi);
680+
true
704681
}
705682
_ => false,
706683
}
707684
}
708685

709686
fn expect_lt(&mut self) {
710-
if !self.eat_lt(true) {
687+
if !self.eat_lt() {
711688
let found_token = self.this_token_to_string();
712689
let token_str = Parser::token_to_string(&token::Lt);
713690
self.fatal(format!("expected `{}`, found `{}`",
@@ -1582,9 +1559,8 @@ impl<'a> Parser<'a> {
15821559
TyTypeof(e)
15831560
} else if self.eat_keyword(keywords::Proc) {
15841561
self.parse_proc_type(Vec::new())
1585-
} else if self.check(&token::Lt) {
1562+
} else if self.eat_lt() {
15861563
// QUALIFIED PATH `<TYPE as TRAIT_REF>::item`
1587-
self.bump();
15881564
let self_type = self.parse_ty_sum();
15891565
self.expect_keyword(keywords::As);
15901566
let trait_ref = self.parse_trait_ref();
@@ -1870,7 +1846,7 @@ impl<'a> Parser<'a> {
18701846
let identifier = self.parse_ident();
18711847

18721848
// Parse types, optionally.
1873-
let parameters = if self.eat_lt(false) {
1849+
let parameters = if self.eat_lt() {
18741850
let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
18751851

18761852
ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
@@ -1931,7 +1907,7 @@ impl<'a> Parser<'a> {
19311907
}
19321908

19331909
// Check for a type segment.
1934-
if self.eat_lt(false) {
1910+
if self.eat_lt() {
19351911
// Consumed `a::b::<`, go look for types
19361912
let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
19371913
segments.push(ast::PathSegment {

src/libtime/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ pub fn get_time() -> Timespec {
149149
// A FILETIME contains a 64-bit value representing the number of
150150
// hectonanosecond (100-nanosecond) intervals since 1601-01-01T00:00:00Z.
151151
// http://support.microsoft.com/kb/167296/en-us
152-
let ns_since_1601 = ((time.dwHighDateTime as u64 << 32) |
153-
(time.dwLowDateTime as u64 << 0)) / 10;
152+
let ns_since_1601 = (((time.dwHighDateTime as u64) << 32) |
153+
((time.dwLowDateTime as u64) << 0)) / 10;
154154
let ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
155155

156156
((ns_since_1970 / 1000000) as i64,

src/libunicode/u_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a> Iterator<Utf16Item> for Utf16Items<'a> {
459459
}
460460

461461
// all ok, so lets decode it.
462-
let c = ((u - 0xD800) as u32 << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
462+
let c = (((u - 0xD800) as u32) << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
463463
Some(Utf16Item::ScalarValue(unsafe {mem::transmute(c)}))
464464
}
465465
}

src/test/bench/shootout-reverse-complement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Tables {
6262
}
6363
let mut table16 = [0;1 << 16];
6464
for (i, v) in table16.iter_mut().enumerate() {
65-
*v = table8[i & 255] as u16 << 8 |
65+
*v = (table8[i & 255] as u16) << 8 |
6666
table8[i >> 8] as u16;
6767
}
6868
Tables { table8: table8, table16: table16 }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 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+
#![feature(associated_types)]
12+
13+
trait Foo {
14+
type T;
15+
fn foo() -> Box<<Self as Foo>::T>;
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)