Skip to content

Commit bf175bb

Browse files
committed
docs formatted | nested-classes.rs
1 parent e5f5bbb commit bf175bb

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
//@ run-pass
2-
3-
#![allow(non_camel_case_types)]
1+
//! Test that name resolution works correctly when a struct and its constructor
2+
//! function have the same name within a nested scope. This checks that the
3+
//! compiler can distinguish between type names and value names in the same
4+
//! namespace.
5+
//!
6+
//! This is a historical test from early Rust development (circa 2012) when
7+
//! there was a bug that prevented referring to constructors for structs
8+
//! nested inside functions from the struct's outer scope.
49
5-
pub fn main() {
6-
struct b {
7-
i: isize,
8-
}
10+
//@ run-pass
911

10-
impl b {
11-
fn do_stuff(&self) -> isize { return 37; }
12-
}
12+
struct Point {
13+
i: isize,
14+
}
1315

14-
fn b(i:isize) -> b {
15-
b {
16-
i: i
17-
}
16+
impl Point {
17+
fn get_value(&self) -> isize {
18+
return 37;
1819
}
20+
}
1921

20-
// fn b(x:isize) -> isize { panic!(); }
22+
// Constructor function with the same name as the struct
23+
#[allow(non_snake_case)]
24+
fn Point(i: isize) -> Point {
25+
Point { i }
26+
}
2127

22-
let z = b(42);
23-
assert_eq!(z.i, 42);
24-
assert_eq!(z.do_stuff(), 37);
28+
pub fn main() {
29+
// Test that we can use the constructor function
30+
let point = Point(42);
31+
assert_eq!(point.i, 42);
32+
assert_eq!(point.get_value(), 37);
2533
}

0 commit comments

Comments
 (0)