Skip to content

Commit

Permalink
Feature-gate associated constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
quantheory committed Apr 24, 2015
1 parent 29eb550 commit b1db4ec
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/doc/reference.md
Expand Up @@ -2340,7 +2340,10 @@ The currently implemented features of the reference compiler are:
semantics are likely to change, so this macro usage must be opted
into.

* `associated_types` - Allows type aliases in traits. Experimental.
* `associated_consts` - Allows constants to be defined in `impl` and `trait`
blocks, so that they can be associated with a type or
trait in a similar manner to methods and associated
types.

* `box_patterns` - Allows `box` patterns, the exact semantics of which
is subject to change.
Expand Down
28 changes: 28 additions & 0 deletions src/libsyntax/feature_gate.rs
Expand Up @@ -155,6 +155,10 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[

// Allows use of unary negate on unsigned integers, e.g. -e for e: u8
("negate_unsigned", "1.0.0", Active),

// Allows the definition of associated constants in `trait` or `impl`
// blocks.
("associated_consts", "1.0.0", Active),
];
// (changing above list without updating src/doc/reference.md makes @cmr sad)

Expand Down Expand Up @@ -659,6 +663,30 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
}
visit::walk_fn(self, fn_kind, fn_decl, block, span);
}

fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
match ti.node {
ast::ConstTraitItem(..) => {
self.gate_feature("associated_consts",
ti.span,
"associated constants are experimental")
}
_ => {}
}
visit::walk_trait_item(self, ti);
}

fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
match ii.node {
ast::ConstImplItem(..) => {
self.gate_feature("associated_consts",
ii.span,
"associated constants are experimental")
}
_ => {}
}
visit::walk_impl_item(self, ii);
}
}

fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
Expand Down
2 changes: 2 additions & 0 deletions src/test/auxiliary/associated-const-cc-lib.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

#![crate_type="lib"]

use std::marker::MarkerTrait;
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/associated-const-dead-code.rs
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]
#![deny(dead_code)]

struct MyFoo;
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/associated-const-impl-wrong-type.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

trait Foo: MarkerTrait {
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/associated-const-private-impl.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

mod bar1 {
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/associated-const-upper-case-lint.rs
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]
#![deny(non_upper_case_globals)]
#![allow(dead_code)]

Expand Down
25 changes: 25 additions & 0 deletions src/test/compile-fail/gated-associated_consts.rs
@@ -0,0 +1,25 @@
// Copyright 2015 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.

use std::marker::MarkerTrait;

trait MyTrait: MarkerTrait {
const C: bool;
//~^ associated constants are experimental
//~| add #![feature(associated_consts)] to the crate attributes to enable
}

struct Foo;

impl Foo {
const C: bool = true;
//~^ associated constants are experimental
//~| add #![feature(associated_consts)] to the crate attributes to enable
}
2 changes: 2 additions & 0 deletions src/test/compile-fail/impl-wrong-item-for-trait.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

trait Foo {
fn bar(&self);
const MY_CONST: u32;
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-cross-crate-defaults.rs
Expand Up @@ -10,6 +10,8 @@

// aux-build:associated-const-cc-lib.rs

#![feature(associated_consts)]

extern crate associated_const_cc_lib as foolib;

pub struct LocalFooUseDefault;
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-cross-crate.rs
Expand Up @@ -10,6 +10,8 @@

// aux-build:associated-const-cc-lib.rs

#![feature(associated_consts)]

extern crate associated_const_cc_lib as foolib;

pub struct LocalFoo;
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-in-global-const.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

struct Foo;

impl Foo {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-inherent-impl.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

struct Foo;

impl Foo {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-marks-live-code.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

#![deny(dead_code)]

const GLOBAL_BAR: u32 = 1;
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-match-patterns.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

struct Foo;
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-overwrite-default.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

trait Foo: MarkerTrait {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-public-impl.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

mod bar1 {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-resolution-order.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

struct MyType;
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-self-type.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

trait MyInt: MarkerTrait {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-ufcs-infer-trait.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

trait Foo: MarkerTrait {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-use-default.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

trait Foo: MarkerTrait {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-use-impl-of-same-trait.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

// The main purpose of this test is to ensure that different impls of the same
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

use std::marker::MarkerTrait;

trait Foo: MarkerTrait {
Expand Down

0 comments on commit b1db4ec

Please sign in to comment.