@@ -65,6 +65,7 @@ use require_c_abi_if_variadic;
65
65
use rscope:: { self , UnelidableRscope , RegionScope , ElidableRscope ,
66
66
ObjectLifetimeDefaultRscope , ShiftedRscope , BindingRscope ,
67
67
ElisionFailureInfo , ElidedLifetime } ;
68
+ use rscope:: { AnonTypeScope , MaybeWithAnonTypes } ;
68
69
use util:: common:: { ErrorReported , FN_OUTPUT_NAME } ;
69
70
use util:: nodemap:: { NodeMap , FnvHashSet } ;
70
71
@@ -635,20 +636,21 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
635
636
636
637
fn convert_ty_with_lifetime_elision ( & self ,
637
638
elided_lifetime : ElidedLifetime ,
638
- ty : & hir:: Ty )
639
+ ty : & hir:: Ty ,
640
+ anon_scope : Option < AnonTypeScope > )
639
641
-> Ty < ' tcx >
640
642
{
641
643
match elided_lifetime {
642
644
Ok ( implied_output_region) => {
643
645
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)
645
647
}
646
648
Err ( param_lifetimes) => {
647
649
// All regions must be explicitly specified in the output
648
650
// if the lifetime elision rules do not apply. This saves
649
651
// the user from potentially-confusing errors.
650
652
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)
652
654
}
653
655
}
654
656
}
@@ -665,7 +667,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
665
667
let region_substs =
666
668
self . create_region_substs ( rscope, span, decl_generics, Vec :: new ( ) ) ;
667
669
668
- let binding_rscope = BindingRscope :: new ( ) ;
670
+ let anon_scope = rscope. anon_type_scope ( ) ;
671
+ let binding_rscope = MaybeWithAnonTypes :: new ( BindingRscope :: new ( ) , anon_scope) ;
669
672
let inputs =
670
673
data. inputs . iter ( )
671
674
. 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 {
679
682
680
683
let ( output, output_span) = match data. output {
681
684
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) ,
683
688
output_ty. span )
684
689
}
685
690
None => {
@@ -1703,7 +1708,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
1703
1708
}
1704
1709
hir:: TyBareFn ( ref bf) => {
1705
1710
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) ;
1707
1719
1708
1720
// Find any late-bound regions declared in return type that do
1709
1721
// not appear in the arguments. These are not wellformed.
@@ -1751,10 +1763,17 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
1751
1763
1752
1764
// Create the anonymized type.
1753
1765
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
+ } ;
1755
1774
let ty = tcx. mk_anon ( tcx. map . local_def_id ( ast_ty. id ) , substs) ;
1756
1775
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 `.
1758
1777
let bounds = compute_bounds ( self , ty, bounds, SizedByDefault :: Yes , ast_ty. span ) ;
1759
1778
let predicates = tcx. lift_to_global ( & bounds. predicates ( tcx, ty) ) . unwrap ( ) ;
1760
1779
let predicates = ty:: GenericPredicates {
@@ -1828,36 +1847,40 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
1828
1847
1829
1848
pub fn ty_of_method ( & self ,
1830
1849
sig : & hir:: MethodSig ,
1831
- untransformed_self_ty : Ty < ' tcx > )
1850
+ untransformed_self_ty : Ty < ' tcx > ,
1851
+ anon_scope : Option < AnonTypeScope > )
1832
1852
-> ( & ' 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 )
1839
1859
}
1840
1860
1841
1861
pub fn ty_of_bare_fn ( & self ,
1842
1862
unsafety : hir:: Unsafety ,
1843
1863
abi : abi:: Abi ,
1844
- decl : & hir:: FnDecl )
1864
+ decl : & hir:: FnDecl ,
1865
+ anon_scope : Option < AnonTypeScope > )
1845
1866
-> & ' 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
1847
1868
}
1848
1869
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 )
1855
1878
{
1856
1879
debug ! ( "ty_of_method_or_bare_fn" ) ;
1857
1880
1858
1881
// New region names that appear inside of the arguments of the function
1859
1882
// declaration are bound to that function type.
1860
- let rb = rscope :: BindingRscope :: new ( ) ;
1883
+ let rb = MaybeWithAnonTypes :: new ( BindingRscope :: new ( ) , arg_anon_scope ) ;
1861
1884
1862
1885
// `implied_output_region` is the region that will be assumed for any
1863
1886
// 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 {
1895
1918
let output_ty = match decl. output {
1896
1919
hir:: Return ( ref output) =>
1897
1920
ty:: FnConverging ( self . convert_ty_with_lifetime_elision ( implied_output_region,
1898
- & output) ) ,
1921
+ & output,
1922
+ ret_anon_scope) ) ,
1899
1923
hir:: DefaultReturn ( ..) => ty:: FnConverging ( self . tcx ( ) . mk_nil ( ) ) ,
1900
1924
hir:: NoReturn ( ..) => ty:: FnDiverging
1901
1925
} ;
0 commit comments