Skip to content

Commit d349e87

Browse files
committed
Parse trait object types starting with a lifetime bound
1 parent 2b4c911 commit d349e87

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

src/libsyntax/parse/parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1451,9 +1451,9 @@ impl<'a> Parser<'a> {
14511451
} else if self.eat_keyword(keywords::Impl) {
14521452
// FIXME: figure out priority of `+` in `impl Trait1 + Trait2` (#34511).
14531453
TyKind::ImplTrait(self.parse_ty_param_bounds()?)
1454-
} else if self.check(&token::Question) {
1454+
} else if self.check(&token::Question) ||
1455+
self.check_lifetime() && self.look_ahead(1, |t| t == &token::BinOp(token::Plus)){
14551456
// Bound list (trait object type)
1456-
// Bound lists starting with `'lt` are not currently supported (#40043)
14571457
TyKind::TraitObject(self.parse_ty_param_bounds_common(allow_plus)?)
14581458
} else {
14591459
let msg = format!("expected type, found {}", self.this_token_descr());

src/test/compile-fail/trait-object-macro-matcher.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ macro_rules! m {
1616

1717
fn main() {
1818
m!(Copy + Send + 'static); //~ ERROR the trait `std::marker::Copy` cannot be made into an object
19+
m!('static + Send);
20+
m!('static +); //~ ERROR at least one non-builtin trait is required for an object type
1921
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 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+
// A few contrived examples where lifetime should (or should not) be parsed as an object type.
12+
// Lifetimes parsed as types are still rejected later by semantic checks.
13+
14+
// compile-flags: -Z continue-parse-after-error
15+
16+
// `'static` is a lifetime, `'static +` is a type, `'a` is a type
17+
fn g() where
18+
'static: 'static,
19+
'static +: 'static + Copy,
20+
//~^ ERROR at least one non-builtin trait is required for an object type
21+
{}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 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+
// A few contrived examples where lifetime should (or should not) be parsed as an object type.
12+
// Lifetimes parsed as types are still rejected later by semantic checks.
13+
14+
// compile-flags: -Z continue-parse-after-error
15+
16+
struct S<'a, T>(&'a u8, T);
17+
18+
fn main() {
19+
// `'static` is a lifetime argument, `'static +` is a type argument
20+
let _: S<'static, u8>;
21+
let _: S<'static, 'static +>;
22+
//~^ at least one non-builtin trait is required for an object type
23+
let _: S<'static, 'static>;
24+
//~^ ERROR wrong number of lifetime parameters: expected 1, found 2
25+
//~| ERROR wrong number of type arguments: expected 1, found 0
26+
let _: S<'static +, 'static>;
27+
//~^ ERROR lifetime parameters must be declared prior to type parameters
28+
//~| ERROR at least one non-builtin trait is required for an object type
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 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+
use std::fmt::Display;
12+
13+
static BYTE: u8 = 33;
14+
15+
fn main() {
16+
let x: &('static + Display) = &BYTE;
17+
let y: Box<'static + Display> = Box::new(BYTE);
18+
let xstr = format!("{}", x);
19+
let ystr = format!("{}", y);
20+
assert_eq!(xstr, "33");
21+
assert_eq!(ystr, "33");
22+
}

0 commit comments

Comments
 (0)