@@ -72,6 +72,7 @@ bitflags! {
7272 flags Restrictions : u8 {
7373 const RESTRICTION_STMT_EXPR = 1 << 0 ,
7474 const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1 ,
75+ const NO_NONINLINE_MOD = 1 << 2 ,
7576 }
7677}
7778
@@ -3301,8 +3302,8 @@ impl<'a> Parser<'a> {
33013302 /// Evaluate the closure with restrictions in place.
33023303 ///
33033304 /// After the closure is evaluated, restrictions are reset.
3304- pub fn with_res < F > ( & mut self , r : Restrictions , f : F ) -> PResult < ' a , P < Expr > >
3305- where F : FnOnce ( & mut Self ) -> PResult < ' a , P < Expr > >
3305+ pub fn with_res < F , T > ( & mut self , r : Restrictions , f : F ) -> T
3306+ where F : FnOnce ( & mut Self ) -> T
33063307 {
33073308 let old = self . restrictions ;
33083309 self . restrictions = r;
@@ -3926,7 +3927,9 @@ impl<'a> Parser<'a> {
39263927 }
39273928 } else {
39283929 // FIXME: Bad copy of attrs
3929- match try!( self . parse_item_ ( attrs. clone ( ) , false , true ) ) {
3930+ let restrictions = self . restrictions | Restrictions :: NO_NONINLINE_MOD ;
3931+ match try!( self . with_res ( restrictions,
3932+ |this| this. parse_item_ ( attrs. clone ( ) , false , true ) ) ) {
39303933 Some ( i) => {
39313934 let hi = i. span . hi ;
39323935 let decl = P ( spanned ( lo, hi, DeclKind :: Item ( i) ) ) ;
@@ -5257,11 +5260,8 @@ impl<'a> Parser<'a> {
52575260 self . push_mod_path ( id, outer_attrs) ;
52585261 try!( self . expect ( & token:: OpenDelim ( token:: Brace ) ) ) ;
52595262 let mod_inner_lo = self . span . lo ;
5260- let old_owns_directory = self . owns_directory ;
5261- self . owns_directory = true ;
52625263 let attrs = try!( self . parse_inner_attributes ( ) ) ;
52635264 let m = try!( self . parse_mod_items ( & token:: CloseDelim ( token:: Brace ) , mod_inner_lo) ) ;
5264- self . owns_directory = old_owns_directory;
52655265 self . pop_mod_path ( ) ;
52665266 Ok ( ( id, ItemKind :: Mod ( m) , Some ( attrs) ) )
52675267 }
@@ -5338,7 +5338,17 @@ impl<'a> Parser<'a> {
53385338
53395339 let paths = Parser :: default_submod_path ( id, & dir_path, self . sess . codemap ( ) ) ;
53405340
5341- if !self . owns_directory {
5341+ if self . restrictions . contains ( Restrictions :: NO_NONINLINE_MOD ) {
5342+ let msg =
5343+ "Cannot declare a non-inline module inside a block unless it has a path attribute" ;
5344+ let mut err = self . diagnostic ( ) . struct_span_err ( id_sp, msg) ;
5345+ if paths. path_exists {
5346+ let msg = format ! ( "Maybe `use` the module `{}` instead of redeclaring it" ,
5347+ paths. name) ;
5348+ err. span_note ( id_sp, & msg) ;
5349+ }
5350+ return Err ( err) ;
5351+ } else if !self . owns_directory {
53425352 let mut err = self . diagnostic ( ) . struct_span_err ( id_sp,
53435353 "cannot declare a new module at this location" ) ;
53445354 let this_module = match self . mod_path_stack . last ( ) {
0 commit comments