@@ -65,6 +65,7 @@ use require_c_abi_if_variadic;
6565use rscope:: { self , UnelidableRscope , RegionScope , ElidableRscope ,
6666 ObjectLifetimeDefaultRscope , ShiftedRscope , BindingRscope ,
6767 ElisionFailureInfo , ElidedLifetime } ;
68+ use rscope:: { AnonTypeScope , MaybeWithAnonTypes } ;
6869use util:: common:: { ErrorReported , FN_OUTPUT_NAME } ;
6970use util:: nodemap:: { NodeMap , FnvHashSet } ;
7071
@@ -635,20 +636,21 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
635636
636637 fn convert_ty_with_lifetime_elision ( & self ,
637638 elided_lifetime : ElidedLifetime ,
638- ty : & hir:: Ty )
639+ ty : & hir:: Ty ,
640+ anon_scope : Option < AnonTypeScope > )
639641 -> Ty < ' tcx >
640642 {
641643 match elided_lifetime {
642644 Ok ( implied_output_region) => {
643645 let rb = ElidableRscope :: new ( implied_output_region) ;
644- self . ast_ty_to_ty ( & rb , ty)
646+ self . ast_ty_to_ty ( & MaybeWithAnonTypes :: new ( rb , anon_scope ) , ty)
645647 }
646648 Err ( param_lifetimes) => {
647649 // All regions must be explicitly specified in the output
648650 // if the lifetime elision rules do not apply. This saves
649651 // the user from potentially-confusing errors.
650652 let rb = UnelidableRscope :: new ( param_lifetimes) ;
651- self . ast_ty_to_ty ( & rb , ty)
653+ self . ast_ty_to_ty ( & MaybeWithAnonTypes :: new ( rb , anon_scope ) , ty)
652654 }
653655 }
654656 }
@@ -665,7 +667,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
665667 let region_substs =
666668 self . create_region_substs ( rscope, span, decl_generics, Vec :: new ( ) ) ;
667669
668- let binding_rscope = BindingRscope :: new ( ) ;
670+ let anon_scope = rscope. anon_type_scope ( ) ;
671+ let binding_rscope = MaybeWithAnonTypes :: new ( BindingRscope :: new ( ) , anon_scope) ;
669672 let inputs =
670673 data. inputs . iter ( )
671674 . map ( |a_t| self . ast_ty_arg_to_ty ( & binding_rscope, decl_generics,
@@ -679,7 +682,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
679682
680683 let ( output, output_span) = match data. output {
681684 Some ( ref output_ty) => {
682- ( self . convert_ty_with_lifetime_elision ( implied_output_region, & output_ty) ,
685+ ( self . convert_ty_with_lifetime_elision ( implied_output_region,
686+ & output_ty,
687+ anon_scope) ,
683688 output_ty. span )
684689 }
685690 None => {
@@ -1703,7 +1708,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
17031708 }
17041709 hir:: TyBareFn ( ref bf) => {
17051710 require_c_abi_if_variadic ( tcx, & bf. decl , bf. abi , ast_ty. span ) ;
1706- let bare_fn_ty = self . ty_of_bare_fn ( bf. unsafety , bf. abi , & bf. decl ) ;
1711+ let anon_scope = rscope. anon_type_scope ( ) ;
1712+ let ( bare_fn_ty, _) =
1713+ self . ty_of_method_or_bare_fn ( bf. unsafety ,
1714+ bf. abi ,
1715+ None ,
1716+ & bf. decl ,
1717+ anon_scope,
1718+ anon_scope) ;
17071719
17081720 // Find any late-bound regions declared in return type that do
17091721 // not appear in the arguments. These are not wellformed.
@@ -1751,10 +1763,17 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
17511763
17521764 // Create the anonymized type.
17531765 let def_id = tcx. map . local_def_id ( ast_ty. id ) ;
1754- let substs = tcx. mk_substs ( Substs :: empty ( ) ) ;
1766+ let substs = if let Some ( anon_scope) = rscope. anon_type_scope ( ) {
1767+ anon_scope. fresh_substs ( tcx)
1768+ } else {
1769+ span_err ! ( tcx. sess, ast_ty. span, E0562 ,
1770+ "`impl Trait` not allowed outside of function \
1771+ and inherent method return types") ;
1772+ tcx. mk_substs ( Substs :: empty ( ) )
1773+ } ;
17551774 let ty = tcx. mk_anon ( tcx. map . local_def_id ( ast_ty. id ) , substs) ;
17561775
1757- // Collect the bounds, i.e. the `Trait ` in `impl Trait `.
1776+ // Collect the bounds, i.e. the `A+B+'c ` in `impl A+B+'c `.
17581777 let bounds = compute_bounds ( self , ty, bounds, SizedByDefault :: Yes , ast_ty. span ) ;
17591778 let predicates = tcx. lift_to_global ( & bounds. predicates ( tcx, ty) ) . unwrap ( ) ;
17601779 let predicates = ty:: GenericPredicates {
@@ -1828,36 +1847,40 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
18281847
18291848 pub fn ty_of_method ( & self ,
18301849 sig : & hir:: MethodSig ,
1831- untransformed_self_ty : Ty < ' tcx > )
1850+ untransformed_self_ty : Ty < ' tcx > ,
1851+ anon_scope : Option < AnonTypeScope > )
18321852 -> ( & ' tcx ty:: BareFnTy < ' tcx > , ty:: ExplicitSelfCategory ) {
1833- let ( bare_fn_ty , optional_explicit_self_category ) =
1834- self . ty_of_method_or_bare_fn ( sig. unsafety ,
1835- sig . abi ,
1836- Some ( untransformed_self_ty ) ,
1837- & sig . decl ) ;
1838- ( bare_fn_ty , optional_explicit_self_category )
1853+ self . ty_of_method_or_bare_fn ( sig . unsafety ,
1854+ sig. abi ,
1855+ Some ( untransformed_self_ty ) ,
1856+ & sig . decl ,
1857+ None ,
1858+ anon_scope )
18391859 }
18401860
18411861 pub fn ty_of_bare_fn ( & self ,
18421862 unsafety : hir:: Unsafety ,
18431863 abi : abi:: Abi ,
1844- decl : & hir:: FnDecl )
1864+ decl : & hir:: FnDecl ,
1865+ anon_scope : Option < AnonTypeScope > )
18451866 -> & ' tcx ty:: BareFnTy < ' tcx > {
1846- self . ty_of_method_or_bare_fn ( unsafety, abi, None , decl) . 0
1867+ self . ty_of_method_or_bare_fn ( unsafety, abi, None , decl, None , anon_scope ) . 0
18471868 }
18481869
1849- fn ty_of_method_or_bare_fn < ' a > ( & self ,
1850- unsafety : hir:: Unsafety ,
1851- abi : abi:: Abi ,
1852- opt_untransformed_self_ty : Option < Ty < ' tcx > > ,
1853- decl : & hir:: FnDecl )
1854- -> ( & ' tcx ty:: BareFnTy < ' tcx > , ty:: ExplicitSelfCategory )
1870+ fn ty_of_method_or_bare_fn ( & self ,
1871+ unsafety : hir:: Unsafety ,
1872+ abi : abi:: Abi ,
1873+ opt_untransformed_self_ty : Option < Ty < ' tcx > > ,
1874+ decl : & hir:: FnDecl ,
1875+ arg_anon_scope : Option < AnonTypeScope > ,
1876+ ret_anon_scope : Option < AnonTypeScope > )
1877+ -> ( & ' tcx ty:: BareFnTy < ' tcx > , ty:: ExplicitSelfCategory )
18551878 {
18561879 debug ! ( "ty_of_method_or_bare_fn" ) ;
18571880
18581881 // New region names that appear inside of the arguments of the function
18591882 // declaration are bound to that function type.
1860- let rb = rscope :: BindingRscope :: new ( ) ;
1883+ let rb = MaybeWithAnonTypes :: new ( BindingRscope :: new ( ) , arg_anon_scope ) ;
18611884
18621885 // `implied_output_region` is the region that will be assumed for any
18631886 // region parameters in the return type. In accordance with the rules for
@@ -1895,7 +1918,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
18951918 let output_ty = match decl. output {
18961919 hir:: Return ( ref output) =>
18971920 ty:: FnConverging ( self . convert_ty_with_lifetime_elision ( implied_output_region,
1898- & output) ) ,
1921+ & output,
1922+ ret_anon_scope) ) ,
18991923 hir:: DefaultReturn ( ..) => ty:: FnConverging ( self . tcx ( ) . mk_nil ( ) ) ,
19001924 hir:: NoReturn ( ..) => ty:: FnDiverging
19011925 } ;
0 commit comments