Skip to content

Commit 4972d14

Browse files
authored
Rollup merge of rust-lang#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 ae14fc4 + 8295e4a commit 4972d14

File tree

35 files changed

+351
-366
lines changed

35 files changed

+351
-366
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 5 additions & 4 deletions
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
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
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) }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 5 additions & 3 deletions
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));

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 1 deletion
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

compiler/rustc_middle/src/mir/abstract_const.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 0 additions & 1 deletion
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;

compiler/rustc_middle/src/mir/query.rs

Lines changed: 1 addition & 13 deletions
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
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 4 deletions
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 {}",

compiler/rustc_middle/src/thir.rs

Lines changed: 3 additions & 0 deletions
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)]
Lines changed: 61 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)