Skip to content

Commit 50ecee2

Browse files
committed
Add feature gate for Self and associated types in struct expressions and patterns
1 parent 0ca9967 commit 50ecee2

8 files changed

+52
-0
lines changed

src/librustc_typeck/check/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -3235,6 +3235,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
32353235
}
32363236
Def::Struct(..) | Def::Union(..) | Def::TyAlias(..) |
32373237
Def::AssociatedTy(..) | Def::SelfTy(..) => {
3238+
match def {
3239+
Def::AssociatedTy(..) | Def::SelfTy(..)
3240+
if !self.tcx.sess.features.borrow().more_struct_aliases => {
3241+
emit_feature_err(&self.tcx.sess.parse_sess,
3242+
"more_struct_aliases", path.span, GateIssue::Language,
3243+
"`Self` and associated types in struct \
3244+
expressions and patterns are unstable");
3245+
}
3246+
_ => {}
3247+
}
32383248
match ty.sty {
32393249
ty::TyAdt(adt, substs) if !adt.is_enum() => {
32403250
Some((adt.struct_variant(), adt.did, substs))

src/libsyntax/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ declare_features! (
309309

310310
// Allows field shorthands (`x` meaning `x: x`) in struct literal expressions.
311311
(active, field_init_shorthand, "1.14.0", Some(37340)),
312+
313+
// Allows using `Self` and associated types in struct expressions and patterns.
314+
(active, more_struct_aliases, "1.14.0", Some(37544)),
312315
);
313316

314317
declare_features! (

src/test/compile-fail/struct-path-associated-type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(more_struct_aliases)]
12+
1113
struct S;
1214

1315
trait Tr {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016 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+
struct S;
12+
13+
trait Tr {
14+
type A;
15+
}
16+
17+
fn f<T: Tr<A = S>>() {
18+
let _ = T::A {};
19+
//~^ ERROR `Self` and associated types in struct expressions and patterns are unstable
20+
}
21+
22+
impl S {
23+
fn f() {
24+
let _ = Self {};
25+
//~^ ERROR `Self` and associated types in struct expressions and patterns are unstable
26+
}
27+
}
28+
29+
fn main() {}

src/test/compile-fail/struct-path-self-type-mismatch.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(more_struct_aliases)]
12+
1113
struct Foo<A> { inner: A }
1214

1315
trait Bar { fn bar(); }

src/test/compile-fail/struct-path-self.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(more_struct_aliases)]
12+
1113
struct S;
1214

1315
trait Tr {

src/test/run-pass/struct-path-associated-type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(more_struct_aliases)]
12+
1113
struct S<T, U = u16> {
1214
a: T,
1315
b: U,

src/test/run-pass/struct-path-self.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(more_struct_aliases)]
12+
1113
use std::ops::Add;
1214

1315
struct S<T, U = u16> {

0 commit comments

Comments
 (0)