Skip to content

Commit 955e2b2

Browse files
committed
nits
1 parent 79be080 commit 955e2b2

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

compiler/rustc_middle/src/mir/query.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Values computed by queries that use MIR.
22
33
use crate::mir::{Body, Promoted};
4-
use crate::thir::abstract_const;
54
use crate::ty::{self, Ty, TyCtxt};
65
use rustc_data_structures::sync::Lrc;
76
use rustc_data_structures::vec_map::VecMap;
@@ -432,16 +431,4 @@ impl<'tcx> TyCtxt<'tcx> {
432431
self.mir_for_ctfe(def.did)
433432
}
434433
}
435-
436-
#[inline]
437-
pub fn thir_abstract_const_opt_const_arg(
438-
self,
439-
def: ty::WithOptConstParam<DefId>,
440-
) -> Result<Option<&'tcx [abstract_const::Node<'tcx>]>, ErrorReported> {
441-
if let Some((did, param_did)) = def.as_const_arg() {
442-
self.thir_abstract_const_of_const_arg((did, param_did))
443-
} else {
444-
self.thir_abstract_const(def.did)
445-
}
446-
}
447434
}

compiler/rustc_middle/src/thir/abstract_const.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A subset of a mir body used for const evaluatability checking.
22
use crate::mir;
3-
use crate::ty::{self, Ty};
3+
use crate::ty::{self, Ty, TyCtxt};
4+
use rustc_errors::ErrorReported;
45

56
rustc_index::newtype_index! {
67
/// An index into an `AbstractConst`.
@@ -22,17 +23,31 @@ pub enum Node<'tcx> {
2223

2324
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
2425
pub enum NotConstEvaluatable {
25-
Error(rustc_errors::ErrorReported),
26+
Error(ErrorReported),
2627
MentionsInfer,
2728
MentionsParam,
2829
}
2930

30-
impl From<rustc_errors::ErrorReported> for NotConstEvaluatable {
31-
fn from(e: rustc_errors::ErrorReported) -> NotConstEvaluatable {
31+
impl From<ErrorReported> for NotConstEvaluatable {
32+
fn from(e: ErrorReported) -> NotConstEvaluatable {
3233
NotConstEvaluatable::Error(e)
3334
}
3435
}
3536

3637
TrivialTypeFoldableAndLiftImpls! {
3738
NotConstEvaluatable,
3839
}
40+
41+
impl<'tcx> TyCtxt<'tcx> {
42+
#[inline]
43+
pub fn thir_abstract_const_opt_const_arg(
44+
self,
45+
def: ty::WithOptConstParam<rustc_hir::def_id::DefId>,
46+
) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorReported> {
47+
if let Some((did, param_did)) = def.as_const_arg() {
48+
self.thir_abstract_const_of_const_arg((did, param_did))
49+
} else {
50+
self.thir_abstract_const(def.did)
51+
}
52+
}
53+
}

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use super::*;
1+
use super::{
2+
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
3+
};
4+
use rustc_middle::ty::Const;
5+
26
pub trait Visitor<'a, 'tcx: 'a>: Sized {
37
fn thir(&self) -> &'a Thir<'tcx>;
48

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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;
1211
use rustc_errors::ErrorReported;
1312
use rustc_hir::def::DefKind;
1413
use rustc_index::vec::IndexVec;
@@ -227,8 +226,7 @@ impl<'tcx> AbstractConst<'tcx> {
227226
struct 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

Comments
 (0)