Skip to content

Commit f5ac5ca

Browse files
authored
Rollup merge of #88709 - BoxyUwU:thir-abstract-const, r=lcnr
generic_const_exprs: use thir for abstract consts instead of mir Changes `AbstractConst` building to use `thir` instead of `mir` so that there's less chance of consts unifying when they shouldn't because lowering to mir dropped information (see `abstract-consts-as-cast-5.rs` test) r? `@lcnr`
2 parents a8e3afe + 8295e4a commit f5ac5ca

35 files changed

+351
-366
lines changed

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_middle::middle::cstore::{ForeignModule, LinkagePreference, NativeLib};
2626
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
2727
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2828
use rustc_middle::mir::{self, Body, Promoted};
29+
use rustc_middle::thir;
2930
use rustc_middle::ty::codec::TyDecoder;
3031
use rustc_middle::ty::{self, Ty, TyCtxt, Visibility};
3132
use rustc_serialize::{opaque, Decodable, Decoder};
@@ -541,7 +542,7 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
541542
}
542543
}
543544

544-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [mir::abstract_const::Node<'tcx>] {
545+
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
545546
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Result<Self, String> {
546547
ty::codec::RefDecodable::decode(d)
547548
}
@@ -1196,14 +1197,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11961197
.decode((self, tcx))
11971198
}
11981199

1199-
fn get_mir_abstract_const(
1200+
fn get_thir_abstract_const(
12001201
&self,
12011202
tcx: TyCtxt<'tcx>,
12021203
id: DefIndex,
1203-
) -> Result<Option<&'tcx [mir::abstract_const::Node<'tcx>]>, ErrorReported> {
1204+
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorReported> {
12041205
self.root
12051206
.tables
1206-
.mir_abstract_consts
1207+
.thir_abstract_consts
12071208
.get(self, id)
12081209
.map_or(Ok(None), |v| Ok(Some(v.decode((self, tcx)))))
12091210
}

Diff for: compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
116116
optimized_mir => { tcx.arena.alloc(cdata.get_optimized_mir(tcx, def_id.index)) }
117117
mir_for_ctfe => { tcx.arena.alloc(cdata.get_mir_for_ctfe(tcx, def_id.index)) }
118118
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
119-
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }
119+
thir_abstract_const => { cdata.get_thir_abstract_const(tcx, def_id.index) }
120120
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
121121
const_param_default => { tcx.mk_const(cdata.get_const_param_default(tcx, def_id.index)) }
122122
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_middle::middle::exported_symbols::{
2323
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
2424
};
2525
use rustc_middle::mir::interpret;
26+
use rustc_middle::thir;
2627
use rustc_middle::traits::specialization_graph;
2728
use rustc_middle::ty::codec::TyEncoder;
2829
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
@@ -344,7 +345,7 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
344345
}
345346
}
346347

347-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for &'tcx [mir::abstract_const::Node<'tcx>] {
348+
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
348349
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
349350
(**self).encode(s)
350351
}
@@ -1297,9 +1298,10 @@ impl EncodeContext<'a, 'tcx> {
12971298
if encode_const {
12981299
record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- self.tcx.mir_for_ctfe(def_id));
12991300

1300-
let abstract_const = self.tcx.mir_abstract_const(def_id);
1301+
// FIXME(generic_const_exprs): this feels wrong to have in `encode_mir`
1302+
let abstract_const = self.tcx.thir_abstract_const(def_id);
13011303
if let Ok(Some(abstract_const)) = abstract_const {
1302-
record!(self.tables.mir_abstract_consts[def_id.to_def_id()] <- abstract_const);
1304+
record!(self.tables.thir_abstract_consts[def_id.to_def_id()] <- abstract_const);
13031305
}
13041306
}
13051307
record!(self.tables.promoted_mir[def_id.to_def_id()] <- self.tcx.promoted_mir(def_id));

Diff for: compiler/rustc_metadata/src/rmeta/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_middle::hir::exports::Export;
1515
use rustc_middle::middle::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
1616
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
1717
use rustc_middle::mir;
18+
use rustc_middle::thir;
1819
use rustc_middle::ty::{self, ReprOptions, Ty};
1920
use rustc_serialize::opaque::Encoder;
2021
use rustc_session::config::SymbolManglingVersion;
@@ -305,7 +306,7 @@ define_tables! {
305306
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
306307
mir_for_ctfe: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
307308
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
308-
mir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [mir::abstract_const::Node<'tcx>])>,
309+
thir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [thir::abstract_const::Node<'tcx>])>,
309310
const_defaults: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
310311
unused_generic_params: Table<DefIndex, Lazy<FiniteBitSet<u32>>>,
311312
// `def_keys` and `def_path_hashes` represent a lazy version of a

Diff for: compiler/rustc_middle/src/mir/abstract_const.rs

-38
This file was deleted.

Diff for: compiler/rustc_middle/src/mir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use self::graph_cyclic_cache::GraphIsCyclicCache;
4040
use self::predecessors::{PredecessorCache, Predecessors};
4141
pub use self::query::*;
4242

43-
pub mod abstract_const;
4443
pub mod coverage;
4544
mod generic_graph;
4645
pub mod generic_graphviz;

Diff for: compiler/rustc_middle/src/mir/query.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Values computed by queries that use MIR.
22
3-
use crate::mir::{abstract_const, Body, Promoted};
3+
use crate::mir::{Body, Promoted};
44
use crate::ty::{self, Ty, TyCtxt};
55
use rustc_data_structures::sync::Lrc;
66
use rustc_data_structures::vec_map::VecMap;
@@ -431,16 +431,4 @@ impl<'tcx> TyCtxt<'tcx> {
431431
self.mir_for_ctfe(def.did)
432432
}
433433
}
434-
435-
#[inline]
436-
pub fn mir_abstract_const_opt_const_arg(
437-
self,
438-
def: ty::WithOptConstParam<DefId>,
439-
) -> Result<Option<&'tcx [abstract_const::Node<'tcx>]>, ErrorReported> {
440-
if let Some((did, param_did)) = def.as_const_arg() {
441-
self.mir_abstract_const_of_const_arg((did, param_did))
442-
} else {
443-
self.mir_abstract_const(def.did)
444-
}
445-
}
446434
}

Diff for: compiler/rustc_middle/src/query/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,17 @@ rustc_queries! {
303303
}
304304

305305
/// Try to build an abstract representation of the given constant.
306-
query mir_abstract_const(
306+
query thir_abstract_const(
307307
key: DefId
308-
) -> Result<Option<&'tcx [mir::abstract_const::Node<'tcx>]>, ErrorReported> {
308+
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorReported> {
309309
desc {
310310
|tcx| "building an abstract representation for {}", tcx.def_path_str(key),
311311
}
312312
}
313313
/// Try to build an abstract representation of the given constant.
314-
query mir_abstract_const_of_const_arg(
314+
query thir_abstract_const_of_const_arg(
315315
key: (LocalDefId, DefId)
316-
) -> Result<Option<&'tcx [mir::abstract_const::Node<'tcx>]>, ErrorReported> {
316+
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorReported> {
317317
desc {
318318
|tcx|
319319
"building an abstract representation for the const argument {}",

Diff for: compiler/rustc_middle/src/thir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use rustc_target::asm::InlineAsmRegOrRegClass;
3333
use std::fmt;
3434
use std::ops::Index;
3535

36+
pub mod abstract_const;
37+
pub mod visit;
38+
3639
newtype_index! {
3740
/// An index to an [`Arm`] stored in [`Thir::arms`]
3841
#[derive(HashStable)]

Diff for: compiler/rustc_middle/src/thir/abstract_const.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! A subset of a mir body used for const evaluatability checking.
2+
use crate::mir;
3+
use crate::ty::{self, Ty, TyCtxt};
4+
use rustc_errors::ErrorReported;
5+
6+
rustc_index::newtype_index! {
7+
/// An index into an `AbstractConst`.
8+
pub struct NodeId {
9+
derive [HashStable]
10+
DEBUG_FORMAT = "n{}",
11+
}
12+
}
13+
14+
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
15+
pub enum CastKind {
16+
/// thir::ExprKind::As
17+
As,
18+
/// thir::ExprKind::Use
19+
Use,
20+
}
21+
22+
/// A node of an `AbstractConst`.
23+
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
24+
pub enum Node<'tcx> {
25+
Leaf(&'tcx ty::Const<'tcx>),
26+
Binop(mir::BinOp, NodeId, NodeId),
27+
UnaryOp(mir::UnOp, NodeId),
28+
FunctionCall(NodeId, &'tcx [NodeId]),
29+
Cast(CastKind, NodeId, Ty<'tcx>),
30+
}
31+
32+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
33+
pub enum NotConstEvaluatable {
34+
Error(ErrorReported),
35+
MentionsInfer,
36+
MentionsParam,
37+
}
38+
39+
impl From<ErrorReported> for NotConstEvaluatable {
40+
fn from(e: ErrorReported) -> NotConstEvaluatable {
41+
NotConstEvaluatable::Error(e)
42+
}
43+
}
44+
45+
TrivialTypeFoldableAndLiftImpls! {
46+
NotConstEvaluatable,
47+
}
48+
49+
impl<'tcx> TyCtxt<'tcx> {
50+
#[inline]
51+
pub fn thir_abstract_const_opt_const_arg(
52+
self,
53+
def: ty::WithOptConstParam<rustc_hir::def_id::DefId>,
54+
) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorReported> {
55+
if let Some((did, param_did)) = def.as_const_arg() {
56+
self.thir_abstract_const_of_const_arg((did, param_did))
57+
} else {
58+
self.thir_abstract_const(def.did)
59+
}
60+
}
61+
}

Diff for: compiler/rustc_mir_build/src/thir/visit.rs renamed to compiler/rustc_middle/src/thir/visit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use rustc_middle::thir::{self, *};
1+
use super::{
2+
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
3+
};
24
use rustc_middle::ty::Const;
35

46
pub trait Visitor<'a, 'tcx: 'a>: Sized {
@@ -101,7 +103,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
101103
visitor.visit_expr(&visitor.thir()[field]);
102104
}
103105
}
104-
Adt(box thir::Adt {
106+
Adt(box crate::thir::Adt {
105107
ref fields,
106108
ref base,
107109
adt_def: _,

Diff for: compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod specialization_graph;
99
mod structural_impls;
1010

1111
use crate::infer::canonical::Canonical;
12-
use crate::mir::abstract_const::NotConstEvaluatable;
12+
use crate::thir::abstract_const::NotConstEvaluatable;
1313
use crate::ty::subst::SubstsRef;
1414
use crate::ty::{self, AdtKind, Ty, TyCtxt};
1515

Diff for: compiler/rustc_middle/src/ty/codec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::mir::{
1212
self,
1313
interpret::{AllocId, Allocation},
1414
};
15+
use crate::thir;
1516
use crate::ty::subst::SubstsRef;
1617
use crate::ty::{self, List, Ty, TyCtxt};
1718
use rustc_data_structures::fx::FxHashMap;
@@ -362,7 +363,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>,
362363
}
363364
}
364365

365-
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::Node<'tcx>] {
366+
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] {
366367
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
367368
Ok(decoder.tcx().arena.alloc_from_iter(
368369
(0..decoder.read_usize()?)
@@ -372,7 +373,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N
372373
}
373374
}
374375

375-
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::NodeId] {
376+
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] {
376377
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
377378
Ok(decoder.tcx().arena.alloc_from_iter(
378379
(0..decoder.read_usize()?)

Diff for: compiler/rustc_mir_build/src/build/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
4747
// Ensure unsafeck is ran before we steal the THIR.
4848
match def {
4949
ty::WithOptConstParam { did, const_param_did: Some(const_param_did) } => {
50-
tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did))
50+
tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did));
51+
tcx.ensure().thir_abstract_const_of_const_arg((did, const_param_did));
5152
}
5253
ty::WithOptConstParam { did, const_param_did: None } => {
53-
tcx.ensure().thir_check_unsafety(did)
54+
tcx.ensure().thir_check_unsafety(did);
55+
tcx.ensure().thir_abstract_const(did);
5456
}
5557
}
5658

Diff for: compiler/rustc_mir_build/src/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::build::ExprCategory;
2-
use crate::thir::visit::{self, Visitor};
2+
use rustc_middle::thir::visit::{self, Visitor};
33

44
use rustc_errors::struct_span_err;
55
use rustc_hir as hir;

Diff for: compiler/rustc_mir_build/src/thir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ crate mod cx;
1111
crate mod pattern;
1212

1313
mod util;
14-
pub mod visit;

Diff for: compiler/rustc_mir_transform/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ fn mir_promoted(
306306
// this point, before we steal the mir-const result.
307307
// Also this means promotion can rely on all const checks having been done.
308308
let _ = tcx.mir_const_qualif_opt_const_arg(def);
309-
let _ = tcx.mir_abstract_const_opt_const_arg(def.to_global());
310309
let mut body = tcx.mir_const(def).steal();
311310

312311
let mut required_consts = Vec::new();

Diff for: compiler/rustc_privacy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_hir::{AssocItemKind, HirIdSet, Node, PatKind};
1919
use rustc_middle::bug;
2020
use rustc_middle::hir::map::Map;
2121
use rustc_middle::middle::privacy::{AccessLevel, AccessLevels};
22-
use rustc_middle::mir::abstract_const::Node as ACNode;
2322
use rustc_middle::span_bug;
23+
use rustc_middle::thir::abstract_const::Node as ACNode;
2424
use rustc_middle::ty::fold::TypeVisitor;
2525
use rustc_middle::ty::query::Providers;
2626
use rustc_middle::ty::subst::{InternalSubsts, Subst};

Diff for: compiler/rustc_query_impl/src/on_disk_cache.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_index::vec::{Idx, IndexVec};
99
use rustc_middle::dep_graph::{DepNode, DepNodeIndex, SerializedDepNodeIndex};
1010
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
1111
use rustc_middle::mir::{self, interpret};
12+
use rustc_middle::thir;
1213
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1314
use rustc_middle::ty::{self, Ty, TyCtxt};
1415
use rustc_query_system::dep_graph::DepContext;
@@ -921,7 +922,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
921922
}
922923
}
923924

924-
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [mir::abstract_const::Node<'tcx>] {
925+
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
925926
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
926927
RefDecodable::decode(d)
927928
}

0 commit comments

Comments
 (0)