From 7f7888754745696467d6ba8f93ce2b9e50c10b3b Mon Sep 17 00:00:00 2001 From: Sean Patrick Santos Date: Sun, 22 Mar 2015 04:38:42 -0600 Subject: [PATCH] Add previously omitted associated const tests. --- ...rs => associated-const-impl-wrong-type.rs} | 17 +++++--- .../associated-const-upper-case-lint.rs | 21 +++++++++ .../compile-fail/impl-wrong-item-for-trait.rs | 43 +++++++++++++++++++ .../associated-const-marks-live-code.rs | 23 ++++++++++ .../associated-const-resolution-order.rs | 35 +++++++++++++++ ...associated-const-use-impl-of-same-trait.rs | 35 +++++++++++++++ 6 files changed, 167 insertions(+), 7 deletions(-) rename src/test/compile-fail/{impl-type-where-trait-has-method.rs => associated-const-impl-wrong-type.rs} (67%) create mode 100644 src/test/compile-fail/associated-const-upper-case-lint.rs create mode 100644 src/test/compile-fail/impl-wrong-item-for-trait.rs create mode 100644 src/test/run-pass/associated-const-marks-live-code.rs create mode 100644 src/test/run-pass/associated-const-resolution-order.rs create mode 100644 src/test/run-pass/associated-const-use-impl-of-same-trait.rs diff --git a/src/test/compile-fail/impl-type-where-trait-has-method.rs b/src/test/compile-fail/associated-const-impl-wrong-type.rs similarity index 67% rename from src/test/compile-fail/impl-type-where-trait-has-method.rs rename to src/test/compile-fail/associated-const-impl-wrong-type.rs index eb75b82424d7..a7b2abc99e5f 100644 --- a/src/test/compile-fail/impl-type-where-trait-has-method.rs +++ b/src/test/compile-fail/associated-const-impl-wrong-type.rs @@ -8,14 +8,17 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -trait Foo { - fn bar(&self); +use std::marker::MarkerTrait; + +trait Foo: MarkerTrait { + const BAR: u32; } -impl Foo for u32 { - //~^ ERROR not all trait items implemented, missing: `bar` - type bar = u64; - //~^ ERROR item `bar` is an associated type, which doesn't match its trait `Foo` +struct SignedBar; + +impl Foo for SignedBar { + const BAR: i32 = -1; + //~^ ERROR E0326 } -fn main () {} +fn main() {} diff --git a/src/test/compile-fail/associated-const-upper-case-lint.rs b/src/test/compile-fail/associated-const-upper-case-lint.rs new file mode 100644 index 000000000000..497ff426b2fa --- /dev/null +++ b/src/test/compile-fail/associated-const-upper-case-lint.rs @@ -0,0 +1,21 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(non_upper_case_globals)] +#![allow(dead_code)] + +struct Foo; + +impl Foo { + const not_upper: bool = true; +} +//~^^ ERROR associated constant `not_upper` should have an upper case name such as `NOT_UPPER` + +fn main() {} diff --git a/src/test/compile-fail/impl-wrong-item-for-trait.rs b/src/test/compile-fail/impl-wrong-item-for-trait.rs new file mode 100644 index 000000000000..3757005b91fd --- /dev/null +++ b/src/test/compile-fail/impl-wrong-item-for-trait.rs @@ -0,0 +1,43 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait Foo { + fn bar(&self); + const MY_CONST: u32; +} + +pub struct FooConstForMethod; + +impl Foo for FooConstForMethod { + //~^ ERROR E0046 + const bar: u64 = 1; + //~^ ERROR E0323 + const MY_CONST: u32 = 1; +} + +pub struct FooMethodForConst; + +impl Foo for FooMethodForConst { + //~^ ERROR E0046 + fn bar(&self) {} + fn MY_CONST() {} + //~^ ERROR E0324 +} + +pub struct FooTypeForMethod; + +impl Foo for FooTypeForMethod { + //~^ ERROR E0046 + type bar = u64; + //~^ ERROR E0325 + const MY_CONST: u32 = 1; +} + +fn main () {} diff --git a/src/test/run-pass/associated-const-marks-live-code.rs b/src/test/run-pass/associated-const-marks-live-code.rs new file mode 100644 index 000000000000..ba47649df64d --- /dev/null +++ b/src/test/run-pass/associated-const-marks-live-code.rs @@ -0,0 +1,23 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(dead_code)] + +const GLOBAL_BAR: u32 = 1; + +struct Foo; + +impl Foo { + const BAR: u32 = GLOBAL_BAR; +} + +fn main() { + let _: u32 = Foo::BAR; +} diff --git a/src/test/run-pass/associated-const-resolution-order.rs b/src/test/run-pass/associated-const-resolution-order.rs new file mode 100644 index 000000000000..e42dd25022b6 --- /dev/null +++ b/src/test/run-pass/associated-const-resolution-order.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::marker::MarkerTrait; + +struct MyType; + +impl MyType { + const IMPL_IS_INHERENT: bool = true; +} + +trait MyTrait: MarkerTrait { + const IMPL_IS_INHERENT: bool; + const IMPL_IS_ON_TRAIT: bool; +} + +impl MyTrait for MyType { + const IMPL_IS_INHERENT: bool = false; + const IMPL_IS_ON_TRAIT: bool = true; +} + +fn main() { + // Check that the inherent impl is used before the trait, but that the trait + // can still be accessed. + assert!(::IMPL_IS_INHERENT); + assert!(!::IMPL_IS_INHERENT); + assert!(::IMPL_IS_ON_TRAIT); +} diff --git a/src/test/run-pass/associated-const-use-impl-of-same-trait.rs b/src/test/run-pass/associated-const-use-impl-of-same-trait.rs new file mode 100644 index 000000000000..2f95d4275c56 --- /dev/null +++ b/src/test/run-pass/associated-const-use-impl-of-same-trait.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::marker::MarkerTrait; + +// The main purpose of this test is to ensure that different impls of the same +// trait can refer to each other without setting off the static recursion check +// (as long as there's no actual recursion). + +trait Foo: MarkerTrait { + const BAR: u32; +} + +struct IsFoo1; + +impl Foo for IsFoo1 { + const BAR: u32 = 1; +} + +struct IsFoo2; + +impl Foo for IsFoo2 { + const BAR: u32 = ::BAR; +} + +fn main() { + assert_eq!(::BAR, ::BAR); +}