Skip to content

Commit 4749961

Browse files
committed
Tell rustc about unused bits in Span.
1 parent 1be5c8f commit 4749961

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3308,7 +3308,7 @@ mod size_asserts {
33083308
rustc_data_structures::static_assert_size!(super::QPath<'static>, 24);
33093309
rustc_data_structures::static_assert_size!(super::Ty<'static>, 80);
33103310

3311-
rustc_data_structures::static_assert_size!(super::Item<'static>, 184);
3311+
rustc_data_structures::static_assert_size!(super::Item<'static>, 176);
33123312
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 128);
33133313
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 152);
33143314
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 136);

compiler/rustc_span/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#![feature(nll)]
2323
#![feature(min_specialization)]
2424
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
25+
#![feature(rustc_attrs)]
2526

2627
#[macro_use]
2728
extern crate rustc_macros;

compiler/rustc_span/src/span_encoding.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,22 @@ use rustc_data_structures::fx::FxIndexSet;
7272
// #[cfg_attr(not(bootstrap), rustc_pass_by_value)]
7373
pub struct Span {
7474
base_or_index: u32,
75-
len_or_tag: u16,
75+
len_or_tag: LenOrTag,
7676
ctxt_or_zero: u16,
7777
}
7878

79-
const LEN_TAG: u16 = 0b1000_0000_0000_0000;
79+
// LEN_TAG allows for some extra values at the top. Declare them to rustc to use as niches.
80+
#[rustc_layout_scalar_valid_range_end(0b1000_0000_0000_0000)]
81+
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
82+
struct LenOrTag(u16);
83+
84+
const LEN_TAG: LenOrTag = unsafe { LenOrTag(0b1000_0000_0000_0000) };
8085
const MAX_LEN: u32 = 0b0111_1111_1111_1111;
8186
const MAX_CTXT: u32 = 0b1111_1111_1111_1111;
8287

8388
/// Dummy span, both position and length are zero, syntax context is zero as well.
84-
pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_zero: 0 };
89+
pub const DUMMY_SP: Span =
90+
Span { base_or_index: 0, len_or_tag: unsafe { LenOrTag(0) }, ctxt_or_zero: 0 };
8591

8692
impl Span {
8793
#[inline]
@@ -99,7 +105,11 @@ impl Span {
99105

100106
if len <= MAX_LEN && ctxt2 <= MAX_CTXT && parent.is_none() {
101107
// Inline format.
102-
Span { base_or_index: base, len_or_tag: len as u16, ctxt_or_zero: ctxt2 as u16 }
108+
Span {
109+
base_or_index: base,
110+
len_or_tag: unsafe { LenOrTag(len as u16) },
111+
ctxt_or_zero: ctxt2 as u16,
112+
}
103113
} else {
104114
// Interned format.
105115
let index =
@@ -123,10 +133,10 @@ impl Span {
123133
pub fn data_untracked(self) -> SpanData {
124134
if self.len_or_tag != LEN_TAG {
125135
// Inline format.
126-
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
136+
debug_assert!(self.len_or_tag.0 as u32 <= MAX_LEN);
127137
SpanData {
128138
lo: BytePos(self.base_or_index),
129-
hi: BytePos(self.base_or_index + self.len_or_tag as u32),
139+
hi: BytePos(self.base_or_index + self.len_or_tag.0 as u32),
130140
ctxt: SyntaxContext::from_u32(self.ctxt_or_zero as u32),
131141
parent: None,
132142
}

0 commit comments

Comments
 (0)