@@ -62,7 +62,7 @@ use rustc::ty::wf::object_region_bounds;
6262use rustc_back:: slice;
6363use require_c_abi_if_variadic;
6464use rscope:: { RegionScope , ObjectLifetimeDefaultRscope , ShiftedRscope } ;
65- use rscope:: { AnonTypeScope , MaybeWithAnonTypes , ExplicitRscope } ;
65+ use rscope:: ExplicitRscope ;
6666use util:: common:: { ErrorReported , FN_OUTPUT_NAME } ;
6767use util:: nodemap:: { NodeMap , FxHashSet } ;
6868
@@ -361,8 +361,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
361361 }
362362 hir:: ParenthesizedParameters ( ref data) => {
363363 assert_eq ! ( i, 0 ) ;
364- let ( ty, assoc) =
365- self . convert_parenthesized_parameters ( rscope, substs, data) ;
364+ let ( ty, assoc) = self . convert_parenthesized_parameters ( substs, data) ;
366365 output_assoc_binding = Some ( assoc) ;
367366 ty
368367 }
@@ -416,7 +415,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
416415 vec ! [ output_assoc_binding. unwrap_or_else( || {
417416 // This is an error condition, but we should
418417 // get the associated type binding anyway.
419- self . convert_parenthesized_parameters( rscope , substs, data) . 1
418+ self . convert_parenthesized_parameters( substs, data) . 1
420419 } ) ]
421420 }
422421 } ;
@@ -428,20 +427,17 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
428427 }
429428
430429 fn convert_parenthesized_parameters ( & self ,
431- rscope : & RegionScope ,
432430 region_substs : & [ Kind < ' tcx > ] ,
433431 data : & hir:: ParenthesizedParameterData )
434432 -> ( Ty < ' tcx > , ConvertedBinding < ' tcx > )
435433 {
436- let anon_scope = rscope. anon_type_scope ( ) ;
437- let rscope = MaybeWithAnonTypes :: new ( ExplicitRscope , anon_scope) ;
438434 let inputs = self . tcx ( ) . mk_type_list ( data. inputs . iter ( ) . map ( |a_t| {
439- self . ast_ty_arg_to_ty ( & rscope , None , region_substs, a_t)
435+ self . ast_ty_arg_to_ty ( & ExplicitRscope , None , region_substs, a_t)
440436 } ) ) ;
441437
442438 let ( output, output_span) = match data. output {
443439 Some ( ref output_ty) => {
444- ( self . ast_ty_to_ty ( & rscope , output_ty) , output_ty. span )
440+ ( self . ast_ty_to_ty ( & ExplicitRscope , output_ty) , output_ty. span )
445441 }
446442 None => {
447443 ( self . tcx ( ) . mk_nil ( ) , data. span )
@@ -1309,12 +1305,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
13091305 }
13101306 hir:: TyBareFn ( ref bf) => {
13111307 require_c_abi_if_variadic ( tcx, & bf. decl , bf. abi , ast_ty. span ) ;
1312- let anon_scope = rscope. anon_type_scope ( ) ;
1313- let bare_fn_ty = self . ty_of_method_or_bare_fn ( bf. unsafety ,
1314- bf. abi ,
1315- & bf. decl ,
1316- anon_scope,
1317- anon_scope) ;
1308+ let bare_fn_ty = self . ty_of_fn ( bf. unsafety , bf. abi , & bf. decl ) ;
13181309
13191310 // Find any late-bound regions declared in return type that do
13201311 // not appear in the arguments. These are not wellformed.
@@ -1361,16 +1352,54 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
13611352 hir:: TyImplTrait ( ref bounds) => {
13621353 use collect:: { compute_bounds, SizedByDefault } ;
13631354
1355+ // Figure out if we can allow an `impl Trait` here, by walking up
1356+ // to a `fn` or inherent `impl` method, going only through `Ty`
1357+ // or `TraitRef` nodes (as nothing else should be in types) and
1358+ // ensuring that we reach the `fn`/method signature's return type.
1359+ let mut node_id = ast_ty. id ;
1360+ let fn_decl = loop {
1361+ let parent = tcx. hir . get_parent_node ( node_id) ;
1362+ match tcx. hir . get ( parent) {
1363+ hir:: map:: NodeItem ( & hir:: Item {
1364+ node : hir:: ItemFn ( ref fn_decl, ..) , ..
1365+ } ) => break Some ( fn_decl) ,
1366+
1367+ hir:: map:: NodeImplItem ( & hir:: ImplItem {
1368+ node : hir:: ImplItemKind :: Method ( ref sig, _) , ..
1369+ } ) => {
1370+ match tcx. hir . expect_item ( tcx. hir . get_parent ( parent) ) . node {
1371+ hir:: ItemImpl ( .., None , _, _) => {
1372+ break Some ( & sig. decl )
1373+ }
1374+ _ => break None
1375+ }
1376+ }
1377+
1378+ hir:: map:: NodeTy ( _) | hir:: map:: NodeTraitRef ( _) => { }
1379+
1380+ _ => break None
1381+ }
1382+ node_id = parent;
1383+ } ;
1384+ let allow = fn_decl. map_or ( false , |fd| {
1385+ match fd. output {
1386+ hir:: DefaultReturn ( _) => false ,
1387+ hir:: Return ( ref ty) => ty. id == node_id
1388+ }
1389+ } ) ;
1390+
13641391 // Create the anonymized type.
1365- let def_id = tcx. hir . local_def_id ( ast_ty. id ) ;
1366- if let Some ( anon_scope) = rscope. anon_type_scope ( ) {
1367- let substs = anon_scope. fresh_substs ( self , ast_ty. span ) ;
1392+ if allow {
1393+ let def_id = tcx. hir . local_def_id ( ast_ty. id ) ;
1394+ if let Err ( ErrorReported ) = self . get_generics ( ast_ty. span , def_id) {
1395+ return tcx. types . err ;
1396+ }
1397+ let substs = Substs :: identity_for_item ( tcx, def_id) ;
13681398 let ty = tcx. mk_anon ( tcx. hir . local_def_id ( ast_ty. id ) , substs) ;
13691399
13701400 // Collect the bounds, i.e. the `A+B+'c` in `impl A+B+'c`.
13711401 let bounds = compute_bounds ( self , ty, bounds,
13721402 SizedByDefault :: Yes ,
1373- Some ( anon_scope) ,
13741403 ast_ty. span ) ;
13751404 let predicates = bounds. predicates ( tcx, ty) ;
13761405 let predicates = tcx. lift_to_global ( & predicates) . unwrap ( ) ;
@@ -1450,36 +1479,19 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
14501479 pub fn ty_of_fn ( & self ,
14511480 unsafety : hir:: Unsafety ,
14521481 abi : abi:: Abi ,
1453- decl : & hir:: FnDecl ,
1454- anon_scope : Option < AnonTypeScope > )
1482+ decl : & hir:: FnDecl )
14551483 -> & ' tcx ty:: BareFnTy < ' tcx > {
1456- self . ty_of_method_or_bare_fn ( unsafety, abi, decl, None , anon_scope)
1457- }
1458-
1459- fn ty_of_method_or_bare_fn ( & self ,
1460- unsafety : hir:: Unsafety ,
1461- abi : abi:: Abi ,
1462- decl : & hir:: FnDecl ,
1463- arg_anon_scope : Option < AnonTypeScope > ,
1464- ret_anon_scope : Option < AnonTypeScope > )
1465- -> & ' tcx ty:: BareFnTy < ' tcx >
1466- {
1467- debug ! ( "ty_of_method_or_bare_fn" ) ;
1468-
1469- // New region names that appear inside of the arguments of the function
1470- // declaration are bound to that function type.
1471- let rb = MaybeWithAnonTypes :: new ( ExplicitRscope , arg_anon_scope) ;
1484+ debug ! ( "ty_of_fn" ) ;
14721485
14731486 let input_tys: Vec < Ty > =
1474- decl. inputs . iter ( ) . map ( |a| self . ty_of_arg ( & rb , a, None ) ) . collect ( ) ;
1487+ decl. inputs . iter ( ) . map ( |a| self . ty_of_arg ( & ExplicitRscope , a, None ) ) . collect ( ) ;
14751488
14761489 let output_ty = match decl. output {
1477- hir:: Return ( ref output) =>
1478- self . ast_ty_to_ty ( & MaybeWithAnonTypes :: new ( ExplicitRscope , ret_anon_scope) , output) ,
1490+ hir:: Return ( ref output) => self . ast_ty_to_ty ( & ExplicitRscope , output) ,
14791491 hir:: DefaultReturn ( ..) => self . tcx ( ) . mk_nil ( ) ,
14801492 } ;
14811493
1482- debug ! ( "ty_of_method_or_bare_fn : output_ty={:?}" , output_ty) ;
1494+ debug ! ( "ty_of_fn : output_ty={:?}" , output_ty) ;
14831495
14841496 self . tcx ( ) . mk_bare_fn ( ty:: BareFnTy {
14851497 unsafety : unsafety,
0 commit comments