Skip to content

Enforce the comma after lifetime arguments and before type arguments #24547

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 1 commit into from
Apr 25, 2015
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
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> {
}

pub struct AllTraits<'a> {
borrow: cell::Ref<'a Option<AllTraitsVec>>,
borrow: cell::Ref<'a, Option<AllTraitsVec>>,
idx: usize
}

Expand Down
33 changes: 32 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,9 @@ impl<'a> Parser<'a> {
pub fn bump(&mut self) -> PResult<()> {
self.last_span = self.span;
// Stash token for error recovery (sometimes; clone is not necessarily cheap).
self.last_token = if self.token.is_ident() || self.token.is_path() {
self.last_token = if self.token.is_ident() ||
self.token.is_path() ||
self.token == token::Comma {
Some(Box::new(self.token.clone()))
} else {
None
Expand Down Expand Up @@ -3807,8 +3809,37 @@ impl<'a> Parser<'a> {
fn parse_generic_values_after_lt(&mut self) -> PResult<(Vec<ast::Lifetime>,
Vec<P<Ty>>,
Vec<P<TypeBinding>>)> {
let span_lo = self.span.lo;
let lifetimes = try!(self.parse_lifetimes(token::Comma));

let missing_comma = !lifetimes.is_empty() &&
!self.token.is_like_gt() &&
self.last_token
.as_ref().map_or(true,
|x| &**x != &token::Comma);

if missing_comma {

let msg = format!("expected `,` or `>` after lifetime \
name, found `{}`",
self.this_token_to_string());
self.span_err(self.span, &msg);

let span_hi = self.span.hi;
let span_hi = if self.parse_ty_nopanic().is_ok() {
self.span.hi
} else {
span_hi
};

let msg = format!("did you mean a single argument type &'a Type, \
or did you mean the comma-separated arguments \
'a, Type?");
self.span_note(mk_sp(span_lo, span_hi), &msg);

self.abort_if_errors()
}

// First parse types.
let (types, returned) = try!(self.parse_seq_to_gt_or_return(
Some(token::Comma),
Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ pub enum Token {
}

impl Token {
/// Returns `true` if the token starts with '>'.
pub fn is_like_gt(&self) -> bool {
match *self {
BinOp(Shr) | BinOpEq(Shr) | Gt | Ge => true,
_ => false,
}
}

/// Returns `true` if the token can appear at the start of an expression.
pub fn can_begin_expr(&self) -> bool {
match *self {
Expand Down
46 changes: 46 additions & 0 deletions src/test/compile-fail/issue-20616-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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.

// We need all these 9 issue-20616-N.rs files
// becase we can only catch one parsing error at a time



type Type_1_<'a, T> = &'a T;


type Type_1<'a T> = &'a T; //~ error: expected `,` or `>` after lifetime name, found `T`


//type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`
Copy link
Member

Choose a reason for hiding this comment

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

Why are these tests commented out?



//type Type_3<T> = Box<T,,>; // error: expected type, found `,`


//type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`


type Type_5_<'a> = Type_1_<'a, ()>;


//type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`


//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`


//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
46 changes: 46 additions & 0 deletions src/test/compile-fail/issue-20616-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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.

// We need all these 9 issue-20616-N.rs files
// becase we can only catch one parsing error at a time



type Type_1_<'a, T> = &'a T;


//type Type_1<'a T> = &'a T; // error: expected `,` or `>` after lifetime name, found `T`


type Type_2 = Type_1_<'static ()>; //~ error: expected `,` or `>` after lifetime name, found `(`


//type Type_3<T> = Box<T,,>; // error: expected type, found `,`


//type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`


type Type_5_<'a> = Type_1_<'a, ()>;


//type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`


//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`


//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
46 changes: 46 additions & 0 deletions src/test/compile-fail/issue-20616-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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.

// We need all these 9 issue-20616-N.rs files
// becase we can only catch one parsing error at a time



type Type_1_<'a, T> = &'a T;


//type Type_1<'a T> = &'a T; // error: expected `,` or `>` after lifetime name, found `T`


//type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`


type Type_3<T> = Box<T,,>; //~ error: expected type, found `,`


//type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`


type Type_5_<'a> = Type_1_<'a, ()>;


//type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`


//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`


//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
46 changes: 46 additions & 0 deletions src/test/compile-fail/issue-20616-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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.

// We need all these 9 issue-20616-N.rs files
// becase we can only catch one parsing error at a time



type Type_1_<'a, T> = &'a T;


//type Type_1<'a T> = &'a T; // error: expected `,` or `>` after lifetime name, found `T`


//type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`


//type Type_3<T> = Box<T,,>; // error: expected type, found `,`


type Type_4<T> = Type_1_<'static,, T>; //~ error: expected type, found `,`


type Type_5_<'a> = Type_1_<'a, ()>;


//type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`


//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`


//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
46 changes: 46 additions & 0 deletions src/test/compile-fail/issue-20616-5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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.

// We need all these 9 issue-20616-N.rs files
// becase we can only catch one parsing error at a time



type Type_1_<'a, T> = &'a T;


//type Type_1<'a T> = &'a T; // error: expected `,` or `>` after lifetime name, found `T`


//type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`


//type Type_3<T> = Box<T,,>; // error: expected type, found `,`


//type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`


type Type_5_<'a> = Type_1_<'a, ()>;


type Type_5<'a> = Type_1_<'a, (),,>; //~ error: expected type, found `,`


//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`


//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
46 changes: 46 additions & 0 deletions src/test/compile-fail/issue-20616-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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.

// We need all these 9 issue-20616-N.rs files
// becase we can only catch one parsing error at a time



type Type_1_<'a, T> = &'a T;


//type Type_1<'a T> = &'a T; // error: expected `,` or `>` after lifetime name, found `T`


//type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`


//type Type_3<T> = Box<T,,>; // error: expected type, found `,`


//type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`


type Type_5_<'a> = Type_1_<'a, ()>;


//type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`


type Type_6 = Type_5_<'a,,>; //~ error: expected type, found `,`


//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
Loading