@@ -37,7 +37,7 @@ use crate::hir::{self, ParamName};
37
37
use crate :: hir:: HirVec ;
38
38
use crate :: hir:: map:: { DefKey , DefPathData , Definitions } ;
39
39
use crate :: hir:: def_id:: { DefId , DefIndex , CRATE_DEF_INDEX } ;
40
- use crate :: hir:: def:: { Res , DefKind , PartialRes , PerNS } ;
40
+ use crate :: hir:: def:: { Namespace , Res , DefKind , PartialRes , PerNS } ;
41
41
use crate :: hir:: { GenericArg , ConstArg } ;
42
42
use crate :: hir:: ptr:: P ;
43
43
use crate :: lint:: builtin:: { self , PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES ,
@@ -148,13 +148,6 @@ pub struct LoweringContext<'a> {
148
148
}
149
149
150
150
pub trait Resolver {
151
- /// Resolve a path generated by the lowerer when expanding `for`, `if let`, etc.
152
- fn resolve_ast_path (
153
- & mut self ,
154
- path : & ast:: Path ,
155
- is_value : bool ,
156
- ) -> Res < NodeId > ;
157
-
158
151
/// Obtain resolution for a `NodeId` with a single resolution.
159
152
fn get_partial_res ( & mut self , id : NodeId ) -> Option < PartialRes > ;
160
153
@@ -175,7 +168,7 @@ pub trait Resolver {
175
168
span : Span ,
176
169
crate_root : Option < Symbol > ,
177
170
components : & [ Symbol ] ,
178
- is_value : bool ,
171
+ ns : Namespace ,
179
172
) -> ( ast:: Path , Res < NodeId > ) ;
180
173
181
174
fn has_derives ( & self , node_id : NodeId , derives : SpecialDerives ) -> bool ;
@@ -4447,23 +4440,23 @@ impl<'a> LoweringContext<'a> {
4447
4440
} )
4448
4441
}
4449
4442
4443
+ fn lower_exprs ( & mut self , exprs : & [ AstP < Expr > ] ) -> HirVec < hir:: Expr > {
4444
+ exprs. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( )
4445
+ }
4446
+
4450
4447
fn lower_expr ( & mut self , e : & Expr ) -> hir:: Expr {
4451
4448
let kind = match e. node {
4452
4449
ExprKind :: Box ( ref inner) => hir:: ExprKind :: Box ( P ( self . lower_expr ( inner) ) ) ,
4453
- ExprKind :: Array ( ref exprs) => {
4454
- hir:: ExprKind :: Array ( exprs. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) )
4455
- }
4450
+ ExprKind :: Array ( ref exprs) => hir:: ExprKind :: Array ( self . lower_exprs ( exprs) ) ,
4456
4451
ExprKind :: Repeat ( ref expr, ref count) => {
4457
4452
let expr = P ( self . lower_expr ( expr) ) ;
4458
4453
let count = self . lower_anon_const ( count) ;
4459
4454
hir:: ExprKind :: Repeat ( expr, count)
4460
4455
}
4461
- ExprKind :: Tup ( ref elts) => {
4462
- hir:: ExprKind :: Tup ( elts. iter ( ) . map ( |x| self . lower_expr ( x) ) . collect ( ) )
4463
- }
4456
+ ExprKind :: Tup ( ref elts) => hir:: ExprKind :: Tup ( self . lower_exprs ( elts) ) ,
4464
4457
ExprKind :: Call ( ref f, ref args) => {
4465
4458
let f = P ( self . lower_expr ( f) ) ;
4466
- hir:: ExprKind :: Call ( f, args . iter ( ) . map ( |x| self . lower_expr ( x ) ) . collect ( ) )
4459
+ hir:: ExprKind :: Call ( f, self . lower_exprs ( args ) )
4467
4460
}
4468
4461
ExprKind :: MethodCall ( ref seg, ref args) => {
4469
4462
let hir_seg = P ( self . lower_path_segment (
@@ -4475,7 +4468,7 @@ impl<'a> LoweringContext<'a> {
4475
4468
ImplTraitContext :: disallowed ( ) ,
4476
4469
None ,
4477
4470
) ) ;
4478
- let args = args . iter ( ) . map ( |x| self . lower_expr ( x ) ) . collect ( ) ;
4471
+ let args = self . lower_exprs ( args ) ;
4479
4472
hir:: ExprKind :: MethodCall ( hir_seg, seg. ident . span , args)
4480
4473
}
4481
4474
ExprKind :: Binary ( binop, ref lhs, ref rhs) => {
@@ -5049,17 +5042,9 @@ impl<'a> LoweringContext<'a> {
5049
5042
) ) ;
5050
5043
let arms = hir_vec ! [ pat_arm, break_arm] ;
5051
5044
5052
- P ( self . expr (
5053
- head_sp,
5054
- hir:: ExprKind :: Match (
5055
- next_expr,
5056
- arms,
5057
- hir:: MatchSource :: ForLoopDesugar
5058
- ) ,
5059
- ThinVec :: new ( ) ,
5060
- ) )
5045
+ self . expr_match ( head_sp, next_expr, arms, hir:: MatchSource :: ForLoopDesugar )
5061
5046
} ;
5062
- let match_stmt = self . stmt ( head_sp, hir :: StmtKind :: Expr ( match_expr) ) ;
5047
+ let match_stmt = self . stmt_expr ( head_sp, match_expr) ;
5063
5048
5064
5049
let next_expr = P ( self . expr_ident ( head_sp, next_ident, next_pat_hid) ) ;
5065
5050
@@ -5083,8 +5068,8 @@ impl<'a> LoweringContext<'a> {
5083
5068
) ;
5084
5069
5085
5070
let body_block = self . with_loop_scope ( e. id , |this| this. lower_block ( body, false ) ) ;
5086
- let body_expr = P ( self . expr_block ( body_block, ThinVec :: new ( ) ) ) ;
5087
- let body_stmt = self . stmt ( body. span , hir :: StmtKind :: Expr ( body_expr) ) ;
5071
+ let body_expr = self . expr_block ( body_block, ThinVec :: new ( ) ) ;
5072
+ let body_stmt = self . stmt_expr ( body. span , body_expr) ;
5088
5073
5089
5074
let loop_block = P ( self . block_all (
5090
5075
e. span ,
@@ -5127,8 +5112,10 @@ impl<'a> LoweringContext<'a> {
5127
5112
) ) ;
5128
5113
5129
5114
// This is effectively `{ let _result = ...; _result }`.
5130
- // The construct was introduced in #21984.
5131
- // FIXME(60253): Is this still necessary?
5115
+ // The construct was introduced in #21984 and is necessary to make sure that
5116
+ // temporaries in the `head` expression are dropped and do not leak to the
5117
+ // surrounding scope of the `match` since the `match` is not a terminating scope.
5118
+ //
5132
5119
// Also, add the attributes to the outer returned expr node.
5133
5120
return self . expr_drop_temps ( head_sp, match_expr, e. attrs . clone ( ) )
5134
5121
}
@@ -5254,7 +5241,7 @@ impl<'a> LoweringContext<'a> {
5254
5241
}
5255
5242
5256
5243
fn lower_stmt ( & mut self , s : & Stmt ) -> SmallVec < [ hir:: Stmt ; 1 ] > {
5257
- smallvec ! [ match s. node {
5244
+ let node = match s. node {
5258
5245
StmtKind :: Local ( ref l) => {
5259
5246
let ( l, item_ids) = self . lower_local ( l) ;
5260
5247
let mut ids: SmallVec < [ hir:: Stmt ; 1 ] > = item_ids
@@ -5291,21 +5278,14 @@ impl<'a> LoweringContext<'a> {
5291
5278
} )
5292
5279
. collect ( ) ;
5293
5280
}
5294
- StmtKind :: Expr ( ref e) => {
5295
- hir:: Stmt {
5296
- hir_id: self . lower_node_id( s. id) ,
5297
- node: hir:: StmtKind :: Expr ( P ( self . lower_expr( e) ) ) ,
5298
- span: s. span,
5299
- }
5300
- } ,
5301
- StmtKind :: Semi ( ref e) => {
5302
- hir:: Stmt {
5303
- hir_id: self . lower_node_id( s. id) ,
5304
- node: hir:: StmtKind :: Semi ( P ( self . lower_expr( e) ) ) ,
5305
- span: s. span,
5306
- }
5307
- } ,
5281
+ StmtKind :: Expr ( ref e) => hir:: StmtKind :: Expr ( P ( self . lower_expr ( e) ) ) ,
5282
+ StmtKind :: Semi ( ref e) => hir:: StmtKind :: Semi ( P ( self . lower_expr ( e) ) ) ,
5308
5283
StmtKind :: Mac ( ..) => panic ! ( "Shouldn't exist here" ) ,
5284
+ } ;
5285
+ smallvec ! [ hir:: Stmt {
5286
+ hir_id: self . lower_node_id( s. id) ,
5287
+ node,
5288
+ span: s. span,
5309
5289
} ]
5310
5290
}
5311
5291
@@ -5567,6 +5547,10 @@ impl<'a> LoweringContext<'a> {
5567
5547
hir:: Stmt { span, node, hir_id : self . next_id ( ) }
5568
5548
}
5569
5549
5550
+ fn stmt_expr ( & mut self , span : Span , expr : hir:: Expr ) -> hir:: Stmt {
5551
+ self . stmt ( span, hir:: StmtKind :: Expr ( P ( expr) ) )
5552
+ }
5553
+
5570
5554
fn stmt_let_pat (
5571
5555
& mut self ,
5572
5556
attrs : ThinVec < Attribute > ,
@@ -5717,8 +5701,8 @@ impl<'a> LoweringContext<'a> {
5717
5701
params : Option < P < hir:: GenericArgs > > ,
5718
5702
is_value : bool ,
5719
5703
) -> hir:: Path {
5720
- let ( path , res ) = self . resolver
5721
- . resolve_str_path ( span, self . crate_root , components, is_value ) ;
5704
+ let ns = if is_value { Namespace :: ValueNS } else { Namespace :: TypeNS } ;
5705
+ let ( path , res ) = self . resolver . resolve_str_path ( span, self . crate_root , components, ns ) ;
5722
5706
5723
5707
let mut segments: Vec < _ > = path. segments . iter ( ) . map ( |segment| {
5724
5708
let res = self . expect_full_res ( segment. id ) ;
@@ -6060,23 +6044,23 @@ impl<'a> LoweringContext<'a> {
6060
6044
} ;
6061
6045
6062
6046
let match_stmt = {
6063
- let match_expr = P ( self . expr_match (
6047
+ let match_expr = self . expr_match (
6064
6048
span,
6065
6049
poll_expr,
6066
6050
hir_vec ! [ ready_arm, pending_arm] ,
6067
6051
hir:: MatchSource :: AwaitDesugar ,
6068
- ) ) ;
6069
- self . stmt ( span, hir :: StmtKind :: Expr ( match_expr) )
6052
+ ) ;
6053
+ self . stmt_expr ( span, match_expr)
6070
6054
} ;
6071
6055
6072
6056
let yield_stmt = {
6073
6057
let unit = self . expr_unit ( span) ;
6074
- let yield_expr = P ( self . expr (
6058
+ let yield_expr = self . expr (
6075
6059
span,
6076
6060
hir:: ExprKind :: Yield ( P ( unit) , hir:: YieldSource :: Await ) ,
6077
6061
ThinVec :: new ( ) ,
6078
- ) ) ;
6079
- self . stmt ( span, hir :: StmtKind :: Expr ( yield_expr) )
6062
+ ) ;
6063
+ self . stmt_expr ( span, yield_expr)
6080
6064
} ;
6081
6065
6082
6066
let loop_block = P ( self . block_all (
0 commit comments