Skip to content

Commit fd3eeb8

Browse files
committed
Split left-shift into two less-than for properly parsing associated type syntax.
[breaking-change] With this change any left-shift operator after an identifier such as `as` will be incorrectly interpreted. To handle this you must change: 34 as uint << 3 To: (34 as uint) << 3
1 parent 39d7402 commit fd3eeb8

File tree

9 files changed

+48
-58
lines changed

9 files changed

+48
-58
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
@@ -56,10 +56,10 @@ pub enum InAddr {
5656
pub fn ip_to_inaddr(ip: IpAddr) -> InAddr {
5757
match ip {
5858
Ipv4Addr(a, b, c, d) => {
59-
let ip = (a as u32 << 24) |
60-
(b as u32 << 16) |
61-
(c as u32 << 8) |
62-
(d as u32 << 0);
59+
let ip = ((a as u32) << 24) |
60+
((b as u32) << 16) |
61+
((c as u32) << 8) |
62+
((d as u32) << 0);
6363
In4Addr(libc::in_addr {
6464
s_addr: Int::from_be(ip)
6565
})

src/libsyntax/parse/parser.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -674,47 +674,21 @@ impl<'a> Parser<'a> {
674674

675675
/// Attempt to consume a `<`. If `<<` is seen, replace it with a single
676676
/// `<` and continue. If a `<` is not seen, return false.
677-
///
678-
/// This is meant to be used when parsing generics on a path to get the
679-
/// starting token. The `force` parameter is used to forcefully break up a
680-
/// `<<` token. If `force` is false, then `<<` is only broken when a lifetime
681-
/// shows up next. For example, consider the expression:
682-
///
683-
/// foo as bar << test
684-
///
685-
/// The parser needs to know if `bar <<` is the start of a generic path or if
686-
/// it's a left-shift token. If `test` were a lifetime, then it's impossible
687-
/// for the token to be a left-shift, but if it's not a lifetime, then it's
688-
/// considered a left-shift.
689-
///
690-
/// The reason for this is that the only current ambiguity with `<<` is when
691-
/// parsing closure types:
692-
///
693-
/// foo::<<'a> ||>();
694-
/// impl Foo<<'a> ||>() { ... }
695-
fn eat_lt(&mut self, force: bool) -> bool {
677+
fn eat_lt(&mut self) -> bool {
696678
match self.token {
697679
token::Lt => { self.bump(); true }
698680
token::BinOp(token::Shl) => {
699-
let next_lifetime = self.look_ahead(1, |t| match *t {
700-
token::Lifetime(..) => true,
701-
_ => false,
702-
});
703-
if force || next_lifetime {
704-
let span = self.span;
705-
let lo = span.lo + BytePos(1);
706-
self.replace_token(token::Lt, lo, span.hi);
707-
true
708-
} else {
709-
false
710-
}
681+
let span = self.span;
682+
let lo = span.lo + BytePos(1);
683+
self.replace_token(token::Lt, lo, span.hi);
684+
true
711685
}
712686
_ => false,
713687
}
714688
}
715689

716690
fn expect_lt(&mut self) {
717-
if !self.eat_lt(true) {
691+
if !self.eat_lt() {
718692
let found_token = self.this_token_to_string();
719693
let token_str = Parser::token_to_string(&token::Lt);
720694
self.fatal(format!("expected `{}`, found `{}`",
@@ -1877,7 +1851,7 @@ impl<'a> Parser<'a> {
18771851
let identifier = self.parse_ident();
18781852

18791853
// Parse types, optionally.
1880-
let parameters = if self.eat_lt(false) {
1854+
let parameters = if self.eat_lt() {
18811855
let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
18821856

18831857
ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
@@ -1938,7 +1912,7 @@ impl<'a> Parser<'a> {
19381912
}
19391913

19401914
// Check for a type segment.
1941-
if self.eat_lt(false) {
1915+
if self.eat_lt() {
19421916
// Consumed `a::b::<`, go look for types
19431917
let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
19441918
segments.push(ast::PathSegment {

src/libtime/lib.rs

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

154154
((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
@@ -458,7 +458,7 @@ impl<'a> Iterator<Utf16Item> for Utf16Items<'a> {
458458
}
459459

460460
// all ok, so lets decode it.
461-
let c = ((u - 0xD800) as u32 << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
461+
let c = (((u - 0xD800) as u32) << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
462462
Some(Utf16Item::ScalarValue(unsafe {mem::transmute(c)}))
463463
}
464464
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
#![feature(associated_types)]
11+
12+
trait Foo { type Assoc; }
13+
impl Foo for () { type Assoc = (); }
14+
type OptFooAssoc<T: Foo> = Option<<T as Foo>::Assoc>;
15+
16+
fn main() { }

0 commit comments

Comments
 (0)