Skip to content

Commit

Permalink
libsyntax: Forbid type parameters in field expressions.
Browse files Browse the repository at this point in the history
This breaks code like:

    struct Foo {
        x: int,
    }

    let f: Foo = ...;
    ... f.x::<int> ...

Change this code to not contain an unused type parameter. For example:

    struct Foo {
        x: int,
    }

    let f: Foo = ...;
    ... f.x ...

Closes #18680.

[breaking-change]
  • Loading branch information
pcwalton committed Nov 11, 2014
1 parent 5d29209 commit e6e58e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/libsyntax/parse/parser.rs
Expand Up @@ -2415,9 +2415,16 @@ impl<'a> Parser<'a> {
e = self.mk_expr(lo, hi, nd);
}
_ => {
if !tys.is_empty() {
let last_span = self.last_span;
self.span_err(last_span,
"field expressions may not \
have type parameters");
}

let id = spanned(dot, hi, i);
let field = self.mk_field(e, id, tys);
e = self.mk_expr(lo, hi, field)
e = self.mk_expr(lo, hi, field);
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/type-parameters-in-field-exprs.rs
@@ -0,0 +1,24 @@
// Copyright 2014 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.

struct Foo {
x: int,
y: int,
}

fn main() {
let f = Foo {
x: 1,
y: 2,
};
f.x::<int>;
//~^ ERROR field expressions may not have type parameters
}

5 comments on commit e6e58e4

@bors
Copy link
Contributor

@bors bors commented on e6e58e4 Nov 13, 2014

Choose a reason for hiding this comment

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

saw approval from aturon
at pcwalton@e6e58e4

@bors
Copy link
Contributor

@bors bors commented on e6e58e4 Nov 13, 2014

Choose a reason for hiding this comment

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

merging pcwalton/rust/path-silliness = e6e58e4 into auto

@bors
Copy link
Contributor

@bors bors commented on e6e58e4 Nov 13, 2014

Choose a reason for hiding this comment

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

pcwalton/rust/path-silliness = e6e58e4 merged ok, testing candidate = 82f3838

@bors
Copy link
Contributor

@bors bors commented on e6e58e4 Nov 13, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on e6e58e4 Nov 13, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 82f3838

Please sign in to comment.