Skip to content

Parser accepts bare 'a T in impl header #20616

@aturon

Description

@aturon
Member

The following is currently accepted:

struct JoinGuard<'a, T>;

impl<'a, T> JoinGuard<'a T> {  // note the lack of comma after the second 'a
    fn foo() {}
}

Activity

added
A-parserArea: The lexing & parsing of Rust source code to an AST
on Jan 6, 2015
aturon

aturon commented on Jan 6, 2015

@aturon
MemberAuthor

cc @cmr

bombless

bombless commented on Apr 15, 2015

@bombless
Contributor

I'm not sure if it is a bug or a feature.

type Foo<'a, 'b, T> = &'a &'b T;
fn foo<'a>(_: Foo<'a, 'static ()>) {}

It seems that we can always omit the comma between lifetime bounds and type arguments.

bombless

bombless commented on Apr 15, 2015

@bombless
Contributor

Now I can imagine we may even need a RFC to enforce that comma.

bombless

bombless commented on Apr 17, 2015

@bombless
Contributor
ftxqxd

ftxqxd commented on Apr 17, 2015

@ftxqxd
Contributor

I’m working on a fix. I don’t think this needs an RFC—it’s clearly a bug (it doesn’t work everywhere: type Foo<'a T> = &'a T; doesn’t work) and I doubt much code will be relying on it.

bombless

bombless commented on Apr 17, 2015

@bombless
Contributor

Hmm I already made a patch.. waiting for compiling and make check.

bombless

bombless commented on Apr 17, 2015

@bombless
Contributor
diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs
index c5ff8a1..ccf34bb 100644
--- a/src/librustc_typeck/check/method/suggest.rs
+++ b/src/librustc_typeck/check/method/suggest.rs
@@ -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
 }
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index bef2068..6f75536 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -896,7 +925,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 self.token.clone())
         } else {
             None
@@ -3796,8 +3914,24 @@ impl<'a> Parser<'a> {
     fn parse_generic_values_after_lt(&mut self) -> PResult<(Vec<ast::Lifetime>,
                                                             Vec<P<Ty>>,
                                                             Vec<P<TypeBinding>>)> {
+
+        debug!("###{} {} token: {:?}, lo: {:?}, hi: {:?}", line!(), "generic_values_after_lt", self.token, self.span.lo, self.span.hi);
         let lifetimes = try!(self.parse_lifetimes(token::Comma));

+        if !lifetimes.is_empty() && self.last_token != Some(box token::Comma) {
+            match self.token {
+                token::Gt | token::Ge |
+                token::BinOpEq(token::Shr) | token::BinOp(token::Shr) => {}
+                _ => {
+                    let this_token_str = self.this_token_to_string();
+                    let msg = format!("expected `,` or `>` after lifetime \
+                                      name, found `{}`",
+                                      this_token_str);
+                    return Err(self.fatal(&msg));
+                }
+            }
+        }
+
         // First parse types.
         let (types, returned) = try!(self.parse_seq_to_gt_or_return(
             Some(token::Comma),
ftxqxd

ftxqxd commented on Apr 17, 2015

@ftxqxd
Contributor

Ah OK, you can submit your patch, then. I haven’t even got mine working yet.

added a commit that references this issue on Apr 22, 2015
04bcd11

17 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an AST

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @brson@pnkfelix@aturon@bombless@ftxqxd

      Issue actions

        Parser accepts bare 'a T in impl header · Issue #20616 · rust-lang/rust