Skip to content

Commit 8b847ef

Browse files
committed
add crate-local -Z reference_niches unstable flag (does nothing for now)
1 parent 30ae640 commit 8b847ef

File tree

10 files changed

+61
-3
lines changed

10 files changed

+61
-3
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ bitflags! {
4949
}
5050
}
5151

52+
/// Which niches (beyond the `null` niche) are available on references.
53+
#[derive(Default, Copy, Clone, Hash, Debug, Eq, PartialEq)]
54+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
55+
pub struct ReferenceNichePolicy {
56+
pub size: bool,
57+
pub align: bool,
58+
}
59+
5260
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
5361
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
5462
pub enum IntegerType {

compiler/rustc_interface/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_span::edition::{Edition, DEFAULT_EDITION};
2828
use rustc_span::symbol::sym;
2929
use rustc_span::FileName;
3030
use rustc_span::SourceFileHashAlgorithm;
31+
use rustc_target::abi::ReferenceNichePolicy;
3132
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
3233
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
3334

@@ -819,6 +820,7 @@ fn test_unstable_options_tracking_hash() {
819820
tracked!(profile_emit, Some(PathBuf::from("abc")));
820821
tracked!(profile_sample_use, Some(PathBuf::from("abc")));
821822
tracked!(profiler_runtime, "abc".to_string());
823+
tracked!(reference_niches, Some(ReferenceNichePolicy { size: true, align: false }));
822824
tracked!(relax_elf_relocations, Some(true));
823825
tracked!(relro_level, Some(RelroLevel::Full));
824826
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ provide! { tcx, def_id, other, cdata,
301301
is_profiler_runtime => { cdata.root.profiler_runtime }
302302
required_panic_strategy => { cdata.root.required_panic_strategy }
303303
panic_in_drop_strategy => { cdata.root.panic_in_drop_strategy }
304+
reference_niches_policy => { cdata.root.reference_niches_policy }
304305
extern_crate => {
305306
let r = *cdata.extern_crate.lock();
306307
r.map(|c| &*tcx.arena.alloc(c))

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
673673
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
674674
required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE),
675675
panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop,
676+
reference_niches_policy: tcx.reference_niches_policy(LOCAL_CRATE),
676677
edition: tcx.sess.edition(),
677678
has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE),
678679
has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE),

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_span::edition::Edition;
3232
use rustc_span::hygiene::{ExpnIndex, MacroKind};
3333
use rustc_span::symbol::{Ident, Symbol};
3434
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
35-
use rustc_target::abi::{FieldIdx, VariantIdx};
35+
use rustc_target::abi::{FieldIdx, ReferenceNichePolicy, VariantIdx};
3636
use rustc_target::spec::{PanicStrategy, TargetTriple};
3737

3838
use std::marker::PhantomData;
@@ -251,6 +251,7 @@ pub(crate) struct CrateRoot {
251251
stable_crate_id: StableCrateId,
252252
required_panic_strategy: Option<PanicStrategy>,
253253
panic_in_drop_strategy: PanicStrategy,
254+
reference_niches_policy: ReferenceNichePolicy,
254255
edition: Edition,
255256
has_global_allocator: bool,
256257
has_alloc_error_handler: bool,

compiler/rustc_middle/src/query/erase.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ trivial! {
296296
rustc_span::Symbol,
297297
rustc_span::symbol::Ident,
298298
rustc_target::spec::PanicStrategy,
299+
rustc_target::abi::ReferenceNichePolicy,
299300
rustc_type_ir::Variance,
300301
u32,
301302
usize,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,11 @@ rustc_queries! {
14801480
desc { "getting a crate's configured panic-in-drop strategy" }
14811481
separate_provide_extern
14821482
}
1483+
query reference_niches_policy(_: CrateNum) -> abi::ReferenceNichePolicy {
1484+
fatal_cycle
1485+
desc { "getting a crate's policy for size and alignment niches of references" }
1486+
separate_provide_extern
1487+
}
14831488
query is_no_builtins(_: CrateNum) -> bool {
14841489
fatal_cycle
14851490
desc { "getting whether a crate has `#![no_builtins]`" }

compiler/rustc_session/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,6 +3076,7 @@ pub(crate) mod dep_tracking {
30763076
use rustc_feature::UnstableFeatures;
30773077
use rustc_span::edition::Edition;
30783078
use rustc_span::RealFileName;
3079+
use rustc_target::abi::ReferenceNichePolicy;
30793080
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel};
30803081
use rustc_target::spec::{
30813082
RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
@@ -3171,6 +3172,7 @@ pub(crate) mod dep_tracking {
31713172
OomStrategy,
31723173
LanguageIdentifier,
31733174
TraitSolver,
3175+
ReferenceNichePolicy,
31743176
);
31753177

31763178
impl<T1, T2> DepTrackingHash for (T1, T2)

compiler/rustc_session/src/options.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{lint, EarlyErrorHandler};
66
use rustc_data_structures::profiling::TimePassesFormat;
77
use rustc_errors::ColorConfig;
88
use rustc_errors::{LanguageIdentifier, TerminalUrl};
9+
use rustc_target::abi::ReferenceNichePolicy;
910
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet};
1011
use rustc_target::spec::{
1112
RelocModel, RelroLevel, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
@@ -421,6 +422,8 @@ mod desc {
421422
pub const parse_proc_macro_execution_strategy: &str =
422423
"one of supported execution strategies (`same-thread`, or `cross-thread`)";
423424
pub const parse_dump_solver_proof_tree: &str = "one of: `always`, `on-request`, `on-error`";
425+
pub const parse_opt_reference_niches: &str =
426+
"`null`, or a `,` separated combination of `size` or `align`";
424427
}
425428

426429
mod parse {
@@ -1253,6 +1256,31 @@ mod parse {
12531256
};
12541257
true
12551258
}
1259+
1260+
pub(crate) fn parse_opt_reference_niches(
1261+
slot: &mut Option<ReferenceNichePolicy>,
1262+
v: Option<&str>,
1263+
) -> bool {
1264+
let Some(s) = v else {
1265+
return false;
1266+
};
1267+
1268+
let slot = slot.get_or_insert_default();
1269+
1270+
if s == "null" {
1271+
return true;
1272+
}
1273+
1274+
for opt in s.split(",") {
1275+
match opt {
1276+
"size" => slot.size = true,
1277+
"align" => slot.align = true,
1278+
_ => return false,
1279+
}
1280+
}
1281+
1282+
true
1283+
}
12561284
}
12571285

12581286
options! {
@@ -1698,6 +1726,8 @@ options! {
16981726
"enable queries of the dependency graph for regression testing (default: no)"),
16991727
randomize_layout: bool = (false, parse_bool, [TRACKED],
17001728
"randomize the layout of types (default: no)"),
1729+
reference_niches: Option<ReferenceNichePolicy> = (None, parse_opt_reference_niches, [TRACKED],
1730+
"override the set of discriminant niches that may be exposed by references"),
17011731
relax_elf_relocations: Option<bool> = (None, parse_opt_bool, [TRACKED],
17021732
"whether ELF relocations can be relaxed"),
17031733
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir as hir;
33
use rustc_index::bit_set::BitSet;
44
use rustc_index::{IndexSlice, IndexVec};
55
use rustc_middle::mir::{GeneratorLayout, GeneratorSavedLocal};
6-
use rustc_middle::query::Providers;
6+
use rustc_middle::query::{LocalCrate, Providers};
77
use rustc_middle::ty::layout::{
88
IntegerExt, LayoutCx, LayoutError, LayoutOf, NaiveLayout, TyAndLayout, TyAndNaiveLayout,
99
MAX_SIMD_LANES,
@@ -25,7 +25,14 @@ use crate::errors::{
2525
use crate::layout_sanity_check::sanity_check_layout;
2626

2727
pub fn provide(providers: &mut Providers) {
28-
*providers = Providers { layout_of, naive_layout_of, ..*providers };
28+
*providers = Providers { layout_of, naive_layout_of, reference_niches_policy, ..*providers };
29+
}
30+
31+
#[instrument(skip(tcx), level = "debug")]
32+
fn reference_niches_policy<'tcx>(tcx: TyCtxt<'tcx>, _: LocalCrate) -> ReferenceNichePolicy {
33+
const DEFAULT: ReferenceNichePolicy = ReferenceNichePolicy { size: false, align: false };
34+
35+
tcx.sess.opts.unstable_opts.reference_niches.unwrap_or(DEFAULT)
2936
}
3037

3138
#[instrument(skip(tcx, query), level = "debug")]

0 commit comments

Comments
 (0)