File tree Expand file tree Collapse file tree 1 file changed +26
-18
lines changed Expand file tree Collapse file tree 1 file changed +26
-18
lines changed Original file line number Diff line number Diff line change 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} 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments