@@ -45,13 +45,14 @@ mod rvalue_scopes;
4545mod upvar;
4646mod writeback;
4747
48- pub use diverges:: Diverges ;
49- pub use expectation:: Expectation ;
50- pub use fn_ctxt:: * ;
51- pub use inherited:: { Inherited , InheritedBuilder } ;
48+ pub use fn_ctxt:: FnCtxt ;
49+ pub use inherited:: Inherited ;
5250
5351use crate :: check:: check_fn;
5452use crate :: coercion:: DynamicCoerceMany ;
53+ use crate :: diverges:: Diverges ;
54+ use crate :: expectation:: Expectation ;
55+ use crate :: fn_ctxt:: RawTy ;
5556use crate :: gather_locals:: GatherLocalsVisitor ;
5657use rustc_data_structures:: unord:: UnordSet ;
5758use rustc_errors:: {
@@ -105,10 +106,9 @@ pub struct LocalTy<'tcx> {
105106/// (notably closures), `typeck_results(def_id)` would wind up
106107/// redirecting to the owning function.
107108fn primary_body_of (
108- tcx : TyCtxt < ' _ > ,
109- id : hir:: HirId ,
109+ node : Node < ' _ > ,
110110) -> Option < ( hir:: BodyId , Option < & hir:: Ty < ' _ > > , Option < & hir:: FnSig < ' _ > > ) > {
111- match tcx . hir ( ) . get ( id ) {
111+ match node {
112112 Node :: Item ( item) => match item. kind {
113113 hir:: ItemKind :: Const ( ty, body) | hir:: ItemKind :: Static ( ty, _, body) => {
114114 Some ( ( body, Some ( ty) , None ) )
@@ -142,8 +142,7 @@ fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
142142 }
143143
144144 if let Some ( def_id) = def_id. as_local ( ) {
145- let id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
146- primary_body_of ( tcx, id) . is_some ( )
145+ primary_body_of ( tcx. hir ( ) . get_by_def_id ( def_id) ) . is_some ( )
147146 } else {
148147 false
149148 }
@@ -198,143 +197,140 @@ fn typeck_with_fallback<'tcx>(
198197 }
199198
200199 let id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
200+ let node = tcx. hir ( ) . get ( id) ;
201201 let span = tcx. hir ( ) . span ( id) ;
202202
203203 // Figure out what primary body this item has.
204- let ( body_id, body_ty, fn_sig) = primary_body_of ( tcx , id ) . unwrap_or_else ( || {
204+ let ( body_id, body_ty, fn_sig) = primary_body_of ( node ) . unwrap_or_else ( || {
205205 span_bug ! ( span, "can't type-check body of {:?}" , def_id) ;
206206 } ) ;
207207 let body = tcx. hir ( ) . body ( body_id) ;
208208
209- let typeck_results = Inherited :: build ( tcx, def_id) . enter ( |inh| {
210- let param_env = tcx. param_env ( def_id) ;
211- let param_env = if tcx. has_attr ( def_id. to_def_id ( ) , sym:: rustc_do_not_const_check) {
212- param_env. without_const ( )
209+ let param_env = tcx. param_env ( def_id) ;
210+ let param_env = if tcx. has_attr ( def_id. to_def_id ( ) , sym:: rustc_do_not_const_check) {
211+ param_env. without_const ( )
212+ } else {
213+ param_env
214+ } ;
215+ let inh = Inherited :: new ( tcx, def_id) ;
216+ let mut fcx = FnCtxt :: new ( & inh, param_env, def_id) ;
217+
218+ if let Some ( hir:: FnSig { header, decl, .. } ) = fn_sig {
219+ let fn_sig = if rustc_hir_analysis:: collect:: get_infer_ret_ty ( & decl. output ) . is_some ( ) {
220+ fcx. astconv ( ) . ty_of_fn ( id, header. unsafety , header. abi , decl, None , None )
213221 } else {
214- param_env
222+ tcx . fn_sig ( def_id ) . subst_identity ( )
215223 } ;
216- let mut fcx = FnCtxt :: new ( & inh, param_env, def_id) ;
217-
218- if let Some ( hir:: FnSig { header, decl, .. } ) = fn_sig {
219- let fn_sig = if rustc_hir_analysis:: collect:: get_infer_ret_ty ( & decl. output ) . is_some ( ) {
220- fcx. astconv ( ) . ty_of_fn ( id, header. unsafety , header. abi , decl, None , None )
221- } else {
222- tcx. fn_sig ( def_id) . subst_identity ( )
223- } ;
224224
225- check_abi ( tcx, id, span, fn_sig. abi ( ) ) ;
225+ check_abi ( tcx, id, span, fn_sig. abi ( ) ) ;
226226
227- // Compute the function signature from point of view of inside the fn.
228- let fn_sig = tcx. liberate_late_bound_regions ( def_id. to_def_id ( ) , fn_sig) ;
229- let fn_sig = fcx. normalize ( body. value . span , fn_sig) ;
227+ // Compute the function signature from point of view of inside the fn.
228+ let fn_sig = tcx. liberate_late_bound_regions ( def_id. to_def_id ( ) , fn_sig) ;
229+ let fn_sig = fcx. normalize ( body. value . span , fn_sig) ;
230230
231- check_fn ( & mut fcx, fn_sig, decl, def_id, body, None ) ;
232- } else {
233- let expected_type = body_ty
234- . and_then ( |ty| match ty. kind {
235- hir:: TyKind :: Infer => Some ( fcx. astconv ( ) . ast_ty_to_ty ( ty) ) ,
236- _ => None ,
237- } )
238- . unwrap_or_else ( || match tcx. hir ( ) . get ( id) {
239- Node :: AnonConst ( _) => match tcx. hir ( ) . get ( tcx. hir ( ) . parent_id ( id) ) {
240- Node :: Expr ( & hir:: Expr {
241- kind : hir:: ExprKind :: ConstBlock ( ref anon_const) ,
242- ..
243- } ) if anon_const. hir_id == id => fcx. next_ty_var ( TypeVariableOrigin {
244- kind : TypeVariableOriginKind :: TypeInference ,
245- span,
246- } ) ,
247- Node :: Ty ( & hir:: Ty {
248- kind : hir:: TyKind :: Typeof ( ref anon_const) , ..
249- } ) if anon_const. hir_id == id => fcx. next_ty_var ( TypeVariableOrigin {
250- kind : TypeVariableOriginKind :: TypeInference ,
251- span,
252- } ) ,
253- Node :: Expr ( & hir:: Expr { kind : hir:: ExprKind :: InlineAsm ( asm) , .. } )
254- | Node :: Item ( & hir:: Item { kind : hir:: ItemKind :: GlobalAsm ( asm) , .. } ) => {
255- let operand_ty =
256- asm. operands . iter ( ) . find_map ( |( op, _op_sp) | match op {
257- hir:: InlineAsmOperand :: Const { anon_const }
258- if anon_const. hir_id == id =>
259- {
260- // Inline assembly constants must be integers.
261- Some ( fcx. next_int_var ( ) )
262- }
263- hir:: InlineAsmOperand :: SymFn { anon_const }
264- if anon_const. hir_id == id =>
265- {
266- Some ( fcx. next_ty_var ( TypeVariableOrigin {
267- kind : TypeVariableOriginKind :: MiscVariable ,
268- span,
269- } ) )
270- }
271- _ => None ,
272- } ) ;
273- operand_ty. unwrap_or_else ( fallback)
231+ check_fn ( & mut fcx, fn_sig, decl, def_id, body, None ) ;
232+ } else {
233+ let expected_type = if let Some ( & hir:: Ty { kind : hir:: TyKind :: Infer , span, .. } ) = body_ty {
234+ Some ( fcx. next_ty_var ( TypeVariableOrigin {
235+ kind : TypeVariableOriginKind :: TypeInference ,
236+ span,
237+ } ) )
238+ } else if let Node :: AnonConst ( _) = node {
239+ match tcx. hir ( ) . get ( tcx. hir ( ) . parent_id ( id) ) {
240+ Node :: Expr ( & hir:: Expr {
241+ kind : hir:: ExprKind :: ConstBlock ( ref anon_const) , ..
242+ } ) if anon_const. hir_id == id => Some ( fcx. next_ty_var ( TypeVariableOrigin {
243+ kind : TypeVariableOriginKind :: TypeInference ,
244+ span,
245+ } ) ) ,
246+ Node :: Ty ( & hir:: Ty { kind : hir:: TyKind :: Typeof ( ref anon_const) , .. } )
247+ if anon_const. hir_id == id =>
248+ {
249+ Some ( fcx. next_ty_var ( TypeVariableOrigin {
250+ kind : TypeVariableOriginKind :: TypeInference ,
251+ span,
252+ } ) )
253+ }
254+ Node :: Expr ( & hir:: Expr { kind : hir:: ExprKind :: InlineAsm ( asm) , .. } )
255+ | Node :: Item ( & hir:: Item { kind : hir:: ItemKind :: GlobalAsm ( asm) , .. } ) => {
256+ asm. operands . iter ( ) . find_map ( |( op, _op_sp) | match op {
257+ hir:: InlineAsmOperand :: Const { anon_const } if anon_const. hir_id == id => {
258+ // Inline assembly constants must be integers.
259+ Some ( fcx. next_int_var ( ) )
260+ }
261+ hir:: InlineAsmOperand :: SymFn { anon_const } if anon_const. hir_id == id => {
262+ Some ( fcx. next_ty_var ( TypeVariableOrigin {
263+ kind : TypeVariableOriginKind :: MiscVariable ,
264+ span,
265+ } ) )
274266 }
275- _ => fallback ( ) ,
276- } ,
277- _ => fallback ( ) ,
278- } ) ;
267+ _ => None ,
268+ } )
269+ }
270+ _ => None ,
271+ }
272+ } else {
273+ None
274+ } ;
275+ let expected_type = expected_type. unwrap_or_else ( fallback) ;
279276
280- let expected_type = fcx. normalize ( body. value . span , expected_type) ;
281- fcx. require_type_is_sized ( expected_type, body. value . span , traits:: ConstSized ) ;
277+ let expected_type = fcx. normalize ( body. value . span , expected_type) ;
278+ fcx. require_type_is_sized ( expected_type, body. value . span , traits:: ConstSized ) ;
282279
283- // Gather locals in statics (because of block expressions).
284- GatherLocalsVisitor :: new ( & fcx) . visit_body ( body) ;
280+ // Gather locals in statics (because of block expressions).
281+ GatherLocalsVisitor :: new ( & fcx) . visit_body ( body) ;
285282
286- fcx. check_expr_coercable_to_type ( & body. value , expected_type, None ) ;
283+ fcx. check_expr_coercable_to_type ( & body. value , expected_type, None ) ;
287284
288- fcx. write_ty ( id, expected_type) ;
289- } ;
285+ fcx. write_ty ( id, expected_type) ;
286+ } ;
290287
291- fcx. type_inference_fallback ( ) ;
292-
293- // Even though coercion casts provide type hints, we check casts after fallback for
294- // backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
295- fcx. check_casts ( ) ;
296- fcx. select_obligations_where_possible ( |_| { } ) ;
297-
298- // Closure and generator analysis may run after fallback
299- // because they don't constrain other type variables.
300- // Closure analysis only runs on closures. Therefore they only need to fulfill non-const predicates (as of now)
301- let prev_constness = fcx. param_env . constness ( ) ;
302- fcx. param_env = fcx. param_env . without_const ( ) ;
303- fcx. closure_analyze ( body) ;
304- fcx. param_env = fcx. param_env . with_constness ( prev_constness) ;
305- assert ! ( fcx. deferred_call_resolutions. borrow( ) . is_empty( ) ) ;
306- // Before the generator analysis, temporary scopes shall be marked to provide more
307- // precise information on types to be captured.
308- fcx. resolve_rvalue_scopes ( def_id. to_def_id ( ) ) ;
309-
310- for ( ty, span, code) in fcx. deferred_sized_obligations . borrow_mut ( ) . drain ( ..) {
311- let ty = fcx. normalize ( span, ty) ;
312- fcx. require_type_is_sized ( ty, span, code) ;
313- }
288+ fcx. type_inference_fallback ( ) ;
289+
290+ // Even though coercion casts provide type hints, we check casts after fallback for
291+ // backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
292+ fcx. check_casts ( ) ;
293+ fcx. select_obligations_where_possible ( |_| { } ) ;
294+
295+ // Closure and generator analysis may run after fallback
296+ // because they don't constrain other type variables.
297+ // Closure analysis only runs on closures. Therefore they only need to fulfill non-const predicates (as of now)
298+ let prev_constness = fcx. param_env . constness ( ) ;
299+ fcx. param_env = fcx. param_env . without_const ( ) ;
300+ fcx. closure_analyze ( body) ;
301+ fcx. param_env = fcx. param_env . with_constness ( prev_constness) ;
302+ assert ! ( fcx. deferred_call_resolutions. borrow( ) . is_empty( ) ) ;
303+ // Before the generator analysis, temporary scopes shall be marked to provide more
304+ // precise information on types to be captured.
305+ fcx. resolve_rvalue_scopes ( def_id. to_def_id ( ) ) ;
306+
307+ for ( ty, span, code) in fcx. deferred_sized_obligations . borrow_mut ( ) . drain ( ..) {
308+ let ty = fcx. normalize ( span, ty) ;
309+ fcx. require_type_is_sized ( ty, span, code) ;
310+ }
314311
315- fcx. select_obligations_where_possible ( |_| { } ) ;
312+ fcx. select_obligations_where_possible ( |_| { } ) ;
316313
317- debug ! ( pending_obligations = ?fcx. fulfillment_cx. borrow( ) . pending_obligations( ) ) ;
314+ debug ! ( pending_obligations = ?fcx. fulfillment_cx. borrow( ) . pending_obligations( ) ) ;
318315
319- // This must be the last thing before `report_ambiguity_errors`.
320- fcx. resolve_generator_interiors ( def_id. to_def_id ( ) ) ;
316+ // This must be the last thing before `report_ambiguity_errors`.
317+ fcx. resolve_generator_interiors ( def_id. to_def_id ( ) ) ;
321318
322- debug ! ( pending_obligations = ?fcx. fulfillment_cx. borrow( ) . pending_obligations( ) ) ;
319+ debug ! ( pending_obligations = ?fcx. fulfillment_cx. borrow( ) . pending_obligations( ) ) ;
323320
324- if let None = fcx. infcx . tainted_by_errors ( ) {
325- fcx. report_ambiguity_errors ( ) ;
326- }
321+ if let None = fcx. infcx . tainted_by_errors ( ) {
322+ fcx. report_ambiguity_errors ( ) ;
323+ }
327324
328- if let None = fcx. infcx . tainted_by_errors ( ) {
329- fcx. check_transmutes ( ) ;
330- }
325+ if let None = fcx. infcx . tainted_by_errors ( ) {
326+ fcx. check_transmutes ( ) ;
327+ }
331328
332- fcx. check_asms ( ) ;
329+ fcx. check_asms ( ) ;
333330
334- fcx. infcx . skip_region_resolution ( ) ;
331+ fcx. infcx . skip_region_resolution ( ) ;
335332
336- fcx. resolve_type_vars_in_body ( body)
337- } ) ;
333+ let typeck_results = fcx. resolve_type_vars_in_body ( body) ;
338334
339335 // Consistency check our TypeckResults instance can hold all ItemLocalIds
340336 // it will need to hold.
0 commit comments