88//! In this case we try to build an abstract representation of this constant using
99//! `thir_abstract_const` which can then be checked for structural equality with other
1010//! generic constants mentioned in the `caller_bounds` of the current environment.
11- use rustc_data_structures:: sync:: Lrc ;
1211use rustc_errors:: ErrorReported ;
1312use rustc_hir:: def:: DefKind ;
1413use rustc_index:: vec:: IndexVec ;
@@ -227,8 +226,7 @@ impl<'tcx> AbstractConst<'tcx> {
227226struct AbstractConstBuilder < ' a , ' tcx > {
228227 tcx : TyCtxt < ' tcx > ,
229228 body_id : thir:: ExprId ,
230- /// `Lrc` is used to avoid borrowck difficulties in `recurse_build`
231- body : Lrc < & ' a thir:: Thir < ' tcx > > ,
229+ body : & ' a thir:: Thir < ' tcx > ,
232230 /// The current WIP node tree.
233231 nodes : IndexVec < NodeId , Node < ' tcx > > ,
234232}
@@ -253,8 +251,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
253251 tcx : TyCtxt < ' tcx > ,
254252 ( body, body_id) : ( & ' a thir:: Thir < ' tcx > , thir:: ExprId ) ,
255253 ) -> Result < Option < AbstractConstBuilder < ' a , ' tcx > > , ErrorReported > {
256- let builder =
257- AbstractConstBuilder { tcx, body_id, body : Lrc :: new ( body) , nodes : IndexVec :: new ( ) } ;
254+ let builder = AbstractConstBuilder { tcx, body_id, body, nodes : IndexVec :: new ( ) } ;
258255
259256 struct IsThirPolymorphic < ' a , ' tcx > {
260257 is_poly : bool ,
@@ -328,7 +325,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
328325
329326 fn recurse_build ( & mut self , node : thir:: ExprId ) -> Result < NodeId , ErrorReported > {
330327 use thir:: ExprKind ;
331- let node = & self . body . clone ( ) . exprs [ node] ;
328+ let node = & self . body . exprs [ node] ;
332329 debug ! ( "recurse_build: node={:?}" , node) ;
333330 Ok ( match & node. kind {
334331 // I dont know if handling of these 3 is correct
@@ -338,10 +335,9 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
338335
339336 // subtle: associated consts are literals this arm handles
340337 // `<T as Trait>::ASSOC` as well as `12`
341- & ExprKind :: Literal { literal, .. }
342- | & ExprKind :: StaticRef { literal, .. } => self . nodes . push ( Node :: Leaf ( literal) ) ,
338+ & ExprKind :: Literal { literal, .. } => self . nodes . push ( Node :: Leaf ( literal) ) ,
343339
344- // FIXME(generic_const_exprs) handle `from_hir_call` field
340+ // FIXME(generic_const_exprs): Handle `from_hir_call` field
345341 ExprKind :: Call { fun, args, .. } => {
346342 let fun = self . recurse_build ( * fun) ?;
347343
@@ -361,24 +357,24 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
361357 let arg = self . recurse_build ( arg) ?;
362358 self . nodes . push ( Node :: UnaryOp ( op, arg) )
363359 } ,
364- // this is necessary so that the following compiles:
360+ // This is necessary so that the following compiles:
365361 //
366362 // ```
367363 // fn foo<const N: usize>(a: [(); N + 1]) {
368364 // bar::<{ N + 1 }>();
369365 // }
370366 // ```
371367 ExprKind :: Block { body : thir:: Block { stmts : box [ ] , expr : Some ( e) , .. } } => self . recurse_build ( * e) ?,
372- // ExprKind::Use happens when a `hir::ExprKind::Cast` is a
368+ // ` ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
373369 // "coercion cast" i.e. using a coercion or is a no-op.
374- // this is important so that `N as usize as usize` doesnt unify with `N as usize`
370+ // This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
375371 & ExprKind :: Use { source}
376372 | & ExprKind :: Cast { source } => {
377373 let arg = self . recurse_build ( source) ?;
378374 self . nodes . push ( Node :: Cast ( arg, node. ty ) )
379375 } ,
380376
381- // FIXME(generic_const_exprs) we want to support these
377+ // FIXME(generic_const_exprs): We may want to support these.
382378 ExprKind :: AddressOf { .. }
383379 | ExprKind :: Borrow { .. }
384380 | ExprKind :: Deref { .. }
@@ -390,21 +386,24 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
390386 | ExprKind :: Index { .. }
391387 | ExprKind :: Field { .. }
392388 | ExprKind :: ConstBlock { .. }
393- | ExprKind :: Adt ( _) => return self . error (
389+ | ExprKind :: Adt ( _) => self . error (
394390 Some ( node. span ) ,
395391 "unsupported operation in generic constant, this may be supported in the future" ,
396- ) . map ( |never| never ) ,
392+ ) ? ,
397393
398394 ExprKind :: Match { .. }
399- | ExprKind :: VarRef { .. } //
400- | ExprKind :: UpvarRef { .. } // we dont permit let stmts so...
395+ // we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
396+ | ExprKind :: VarRef { .. }
397+ | ExprKind :: UpvarRef { .. }
401398 | ExprKind :: Closure { .. }
402399 | ExprKind :: Let { .. } // let expressions imply control flow
403400 | ExprKind :: Loop { .. }
404401 | ExprKind :: Assign { .. }
402+ | ExprKind :: StaticRef { .. }
405403 | ExprKind :: LogicalOp { .. }
406- | ExprKind :: Unary { .. } //
407- | ExprKind :: Binary { .. } // we handle valid unary/binary ops above
404+ // we handle valid unary/binary ops above
405+ | ExprKind :: Unary { .. }
406+ | ExprKind :: Binary { .. }
408407 | ExprKind :: Break { .. }
409408 | ExprKind :: Continue { .. }
410409 | ExprKind :: If { .. }
@@ -415,7 +414,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
415414 | ExprKind :: Box { .. } // allocations not allowed in constants
416415 | ExprKind :: AssignOp { .. }
417416 | ExprKind :: InlineAsm { .. }
418- | ExprKind :: Yield { .. } => return self . error ( Some ( node. span ) , "unsupported operation in generic constant" ) . map ( |never| never ) ,
417+ | ExprKind :: Yield { .. } => self . error ( Some ( node. span ) , "unsupported operation in generic constant" ) ? ,
419418 } )
420419 }
421420}
0 commit comments