Skip to content

Commit

Permalink
Rollup merge of rust-lang#37547 - petrochenkov:selfgate, r=nikomatsakis
Browse files Browse the repository at this point in the history
Feature gate Self and associated types in struct expressions and patterns

cc rust-lang#37544
Fixes rust-lang#37035 (comment)
r? @nikomatsakis
  • Loading branch information
Jonathan Turner authored Nov 5, 2016
2 parents eecc5bf + 50ecee2 commit c8ff041
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3252,6 +3252,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
Def::Struct(..) | Def::Union(..) | Def::TyAlias(..) |
Def::AssociatedTy(..) | Def::SelfTy(..) => {
match def {
Def::AssociatedTy(..) | Def::SelfTy(..)
if !self.tcx.sess.features.borrow().more_struct_aliases => {
emit_feature_err(&self.tcx.sess.parse_sess,
"more_struct_aliases", path.span, GateIssue::Language,
"`Self` and associated types in struct \
expressions and patterns are unstable");
}
_ => {}
}
match ty.sty {
ty::TyAdt(adt, substs) if !adt.is_enum() => {
Some((adt.struct_variant(), adt.did, substs))
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ declare_features! (

// Allows field shorthands (`x` meaning `x: x`) in struct literal expressions.
(active, field_init_shorthand, "1.14.0", Some(37340)),

// Allows using `Self` and associated types in struct expressions and patterns.
(active, more_struct_aliases, "1.14.0", Some(37544)),
);

declare_features! (
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/struct-path-associated-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(more_struct_aliases)]

struct S;

trait Tr {
Expand Down
29 changes: 29 additions & 0 deletions src/test/compile-fail/struct-path-self-feature-gate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 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 S;

trait Tr {
type A;
}

fn f<T: Tr<A = S>>() {
let _ = T::A {};
//~^ ERROR `Self` and associated types in struct expressions and patterns are unstable
}

impl S {
fn f() {
let _ = Self {};
//~^ ERROR `Self` and associated types in struct expressions and patterns are unstable
}
}

fn main() {}
2 changes: 2 additions & 0 deletions src/test/compile-fail/struct-path-self-type-mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(more_struct_aliases)]

struct Foo<A> { inner: A }

trait Bar { fn bar(); }
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/struct-path-self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(more_struct_aliases)]

struct S;

trait Tr {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/struct-path-associated-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(more_struct_aliases)]

struct S<T, U = u16> {
a: T,
b: U,
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/struct-path-self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(more_struct_aliases)]

use std::ops::Add;

struct S<T, U = u16> {
Expand Down

0 comments on commit c8ff041

Please sign in to comment.