Skip to content

Commit 833dc7e

Browse files
committed
Address attribute naming and use Bound enum
1 parent a2c924b commit 833dc7e

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

src/libcore/nonzero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ops::CoerceUnsized;
1515
/// A wrapper type for raw pointers and integers that will never be
1616
/// NULL or 0 that might allow certain optimizations.
1717
#[cfg_attr(stage0, lang = "non_zero")]
18-
#[cfg_attr(not(stage0), rustc_layout_scalar_range_start(1))]
18+
#[cfg_attr(not(stage0), rustc_layout_scalar_valid_range_start(1))]
1919
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
2020
#[repr(transparent)]
2121
pub(crate) struct NonZero<T>(pub(crate) T);

src/librustc/ty/context.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use std::collections::hash_map::{self, Entry};
6666
use std::hash::{Hash, Hasher};
6767
use std::fmt;
6868
use std::mem;
69-
use std::ops::Deref;
69+
use std::ops::{Deref, Bound};
7070
use std::iter;
7171
use std::sync::mpsc;
7272
use std::sync::Arc;
@@ -1083,27 +1083,24 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10831083
interned
10841084
}
10851085

1086-
/// Returns a range of the start/end indices specified with the `rustc_layout_scalar_range`
1087-
/// attribute. Missing range ends may be denoted by `None` and will just use the max/min of
1088-
/// the type.
1089-
pub fn layout_scalar_range(self, def_id: DefId) -> Option<(Option<u128>, Option<u128>)> {
1086+
/// Returns a range of the start/end indices specified with the
1087+
/// `rustc_layout_scalar_valid_range` attribute.
1088+
pub fn layout_scalar_valid_range(self, def_id: DefId) -> (Bound<u128>, Bound<u128>) {
10901089
let attrs = self.get_attrs(def_id);
1091-
let get = |name| -> Option<u128> {
1092-
let attr = attrs.iter().find(|a| a.check_name(name))?;
1093-
for meta in attr.meta_item_list().expect("rustc_layout_scalar_range takes args") {
1094-
match meta.literal().expect("rustc_layout_scalar_range attribute takes lit").node {
1095-
ast::LitKind::Int(a, _) => return Some(a),
1096-
_ => span_bug!(attr.span, "rustc_layout_scalar_range expects integer arg"),
1090+
let get = |name| {
1091+
let attr = match attrs.iter().find(|a| a.check_name(name)) {
1092+
Some(attr) => attr,
1093+
None => return Bound::Unbounded,
1094+
};
1095+
for meta in attr.meta_item_list().expect("rustc_layout_scalar_valid_range takes args") {
1096+
match meta.literal().expect("attribute takes lit").node {
1097+
ast::LitKind::Int(a, _) => return Bound::Included(a),
1098+
_ => span_bug!(attr.span, "rustc_layout_scalar_valid_range expects int arg"),
10971099
}
10981100
}
1099-
bug!("no arguments to `rustc_layout_scalar_range` attribute");
1101+
span_bug!(attr.span, "no arguments to `rustc_layout_scalar_valid_range` attribute");
11001102
};
1101-
let start = get("rustc_layout_scalar_range_start");
1102-
let end = get("rustc_layout_scalar_range_end");
1103-
if start.is_none() && end.is_none() {
1104-
return None;
1105-
}
1106-
Some((start, end))
1103+
(get("rustc_layout_scalar_valid_range_start"), get("rustc_layout_scalar_valid_range_end"))
11071104
}
11081105

11091106
pub fn lift<T: ?Sized + Lift<'tcx>>(self, value: &T) -> Option<T::Lifted> {

src/librustc/ty/layout.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::fmt;
2020
use std::i128;
2121
use std::iter;
2222
use std::mem;
23+
use std::ops::Bound;
2324

2425
use ich::StableHashingContext;
2526
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
@@ -761,20 +762,28 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
761762

762763
let mut st = univariant_uninterned(&variants[v], &def.repr, kind)?;
763764
st.variants = Variants::Single { index: v };
764-
if let Some((start, end)) = self.tcx.layout_scalar_range(def.did) {
765-
match st.abi {
766-
Abi::Scalar(ref mut scalar) |
767-
Abi::ScalarPair(ref mut scalar, _) => {
768-
let start = start.unwrap_or(*scalar.valid_range.start());
769-
let end = end.unwrap_or(*scalar.valid_range.end());
770-
scalar.valid_range = start..=end;
765+
let (start, end) = self.tcx.layout_scalar_valid_range(def.did);
766+
match st.abi {
767+
Abi::Scalar(ref mut scalar) |
768+
Abi::ScalarPair(ref mut scalar, _) => {
769+
// the asserts ensure that we are not using the
770+
// `#[rustc_layout_scalar_valid_range(n)]`
771+
// attribute to widen the range of anything as that would probably
772+
// result in UB somewhere
773+
if let Bound::Included(start) = start {
774+
assert!(*scalar.valid_range.start() <= start);
775+
scalar.valid_range = start..=*scalar.valid_range.end();
776+
}
777+
if let Bound::Included(end) = end {
778+
assert!(*scalar.valid_range.end() >= end);
779+
scalar.valid_range = *scalar.valid_range.start()..=end;
771780
}
772-
_ => bug!(
773-
"nonscalar layout for rustc_layout_scalar_range type {:?}: {:#?}",
774-
def,
775-
st,
776-
),
777781
}
782+
_ => bug!(
783+
"nonscalar layout for layout_scalar_valid_range type {:?}: {:#?}",
784+
def,
785+
st,
786+
),
778787
}
779788
return Ok(tcx.intern_layout(st));
780789
}
@@ -1353,13 +1362,12 @@ impl<'a, 'tcx> SizeSkeleton<'tcx> {
13531362
if def.variants.len() == 1 {
13541363
if let Some(SizeSkeleton::Pointer { non_zero, tail }) = v0 {
13551364
return Ok(SizeSkeleton::Pointer {
1356-
non_zero: non_zero ||
1357-
tcx.layout_scalar_range(def.did).map_or(false, |(start, end)| {
1358-
// `n..` for `n > 0` or `n..m` for `n > 0 && m > n`
1359-
start.map_or(true, |start| start > 0 && end.map_or(true, |end| {
1360-
end > start
1361-
}))
1362-
}),
1365+
non_zero: non_zero || match tcx.layout_scalar_valid_range(def.did) {
1366+
(Bound::Included(start), Bound::Unbounded) => start > 0,
1367+
(Bound::Included(start), Bound::Included(end)) =>
1368+
0 < start && start < end,
1369+
_ => false,
1370+
},
13631371
tail,
13641372
});
13651373
} else {

src/librustc_data_structures/indexed_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ macro_rules! newtype_index {
9797
@vis [$v:vis]
9898
@debug_format [$debug_format:tt]) => (
9999
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
100-
#[rustc_layout_scalar_range_end($max)]
100+
#[rustc_layout_scalar_valid_range_end($max)]
101101
$v struct $type {
102102
private: u32
103103
}

0 commit comments

Comments
 (0)