Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement associated constants #23606

Merged
merged 8 commits into from
Apr 27, 2015
Prev Previous commit
Next Next commit
Feature-gate associated constants.
  • Loading branch information
quantheory committed Apr 24, 2015
commit b1db4ec3d0a96a1e83d74fbc7f99dc3be054f4d8
5 changes: 4 additions & 1 deletion src/doc/reference.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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(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
Original file line number Diff line number Diff line change
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
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(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
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(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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
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(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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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(associated_consts)]

struct Foo;

impl Foo {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-inherent-impl.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(associated_consts)]

struct Foo;

impl Foo {
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/associated-const-marks-live-code.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(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
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(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
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(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
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(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
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(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
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(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
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(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
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(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
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(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
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(associated_consts)]

use std::marker::MarkerTrait;

trait Foo: MarkerTrait {
Expand Down