Skip to content

Commit 23cd0bd

Browse files
committed
Add previously omitted associated const tests.
1 parent e18378d commit 23cd0bd

6 files changed

+167
-7
lines changed

src/test/compile-fail/impl-type-where-trait-has-method.rs renamed to src/test/compile-fail/associated-const-impl-wrong-type.rs

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

11-
trait Foo {
12-
fn bar(&self);
11+
use std::marker::MarkerTrait;
12+
13+
trait Foo: MarkerTrait {
14+
const BAR: u32;
1315
}
1416

15-
impl Foo for u32 {
16-
//~^ ERROR not all trait items implemented, missing: `bar`
17-
type bar = u64;
18-
//~^ ERROR item `bar` is an associated type, which doesn't match its trait `Foo`
17+
struct SignedBar;
18+
19+
impl Foo for SignedBar {
20+
const BAR: i32 = -1;
21+
//~^ ERROR E0326
1922
}
2023

21-
fn main () {}
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 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+
#![deny(non_upper_case_globals)]
12+
#![allow(dead_code)]
13+
14+
struct Foo;
15+
16+
impl Foo {
17+
const not_upper: bool = true;
18+
}
19+
//~^^ ERROR associated constant `not_upper` should have an upper case name such as `NOT_UPPER`
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2015 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+
trait Foo {
12+
fn bar(&self);
13+
const MY_CONST: u32;
14+
}
15+
16+
pub struct FooConstForMethod;
17+
18+
impl Foo for FooConstForMethod {
19+
//~^ ERROR E0046
20+
const bar: u64 = 1;
21+
//~^ ERROR E0323
22+
const MY_CONST: u32 = 1;
23+
}
24+
25+
pub struct FooMethodForConst;
26+
27+
impl Foo for FooMethodForConst {
28+
//~^ ERROR E0046
29+
fn bar(&self) {}
30+
fn MY_CONST() {}
31+
//~^ ERROR E0324
32+
}
33+
34+
pub struct FooTypeForMethod;
35+
36+
impl Foo for FooTypeForMethod {
37+
//~^ ERROR E0046
38+
type bar = u64;
39+
//~^ ERROR E0325
40+
const MY_CONST: u32 = 1;
41+
}
42+
43+
fn main () {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2015 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+
#![deny(dead_code)]
12+
13+
const GLOBAL_BAR: u32 = 1;
14+
15+
struct Foo;
16+
17+
impl Foo {
18+
const BAR: u32 = GLOBAL_BAR;
19+
}
20+
21+
fn main() {
22+
let _: u32 = Foo::BAR;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
use std::marker::MarkerTrait;
12+
13+
struct MyType;
14+
15+
impl MyType {
16+
const IMPL_IS_INHERENT: bool = true;
17+
}
18+
19+
trait MyTrait: MarkerTrait {
20+
const IMPL_IS_INHERENT: bool;
21+
const IMPL_IS_ON_TRAIT: bool;
22+
}
23+
24+
impl MyTrait for MyType {
25+
const IMPL_IS_INHERENT: bool = false;
26+
const IMPL_IS_ON_TRAIT: bool = true;
27+
}
28+
29+
fn main() {
30+
// Check that the inherent impl is used before the trait, but that the trait
31+
// can still be accessed.
32+
assert!(<MyType>::IMPL_IS_INHERENT);
33+
assert!(!<MyType as MyTrait>::IMPL_IS_INHERENT);
34+
assert!(<MyType>::IMPL_IS_ON_TRAIT);
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
use std::marker::MarkerTrait;
12+
13+
// The main purpose of this test is to ensure that different impls of the same
14+
// trait can refer to each other without setting off the static recursion check
15+
// (as long as there's no actual recursion).
16+
17+
trait Foo: MarkerTrait {
18+
const BAR: u32;
19+
}
20+
21+
struct IsFoo1;
22+
23+
impl Foo for IsFoo1 {
24+
const BAR: u32 = 1;
25+
}
26+
27+
struct IsFoo2;
28+
29+
impl Foo for IsFoo2 {
30+
const BAR: u32 = <IsFoo1 as Foo>::BAR;
31+
}
32+
33+
fn main() {
34+
assert_eq!(<IsFoo1>::BAR, <IsFoo2 as Foo>::BAR);
35+
}

0 commit comments

Comments
 (0)