1
Fork 0

Restrict access to the private field of newtype indexes

This commit is contained in:
Oli Scherer 2023-01-31 12:44:15 +00:00
parent 92d727796b
commit 93740f9493
2 changed files with 8 additions and 8 deletions

View file

@ -104,7 +104,7 @@ impl Parse for Newtype {
#gate_rustc_only #gate_rustc_only
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for #name { impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for #name {
fn encode(&self, e: &mut E) { fn encode(&self, e: &mut E) {
e.emit_u32(self.private); e.emit_u32(self.as_u32());
} }
} }
} }
@ -164,7 +164,7 @@ impl Parse for Newtype {
#[inline] #[inline]
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool { fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
if #max_val < u32::MAX { if #max_val < u32::MAX {
l.map(|i| i.private).unwrap_or(#max_val+1) == r.map(|i| i.private).unwrap_or(#max_val+1) l.map(|i| i.as_u32()).unwrap_or(#max_val+1) == r.map(|i| i.as_u32()).unwrap_or(#max_val+1)
} else { } else {
match (l, r) { match (l, r) {
(Some(l), Some(r)) => r == l, (Some(l), Some(r)) => r == l,
@ -188,7 +188,7 @@ impl Parse for Newtype {
#[cfg_attr(#gate_rustc_only_cfg, rustc_layout_scalar_valid_range_end(#max))] #[cfg_attr(#gate_rustc_only_cfg, rustc_layout_scalar_valid_range_end(#max))]
#[cfg_attr(#gate_rustc_only_cfg, rustc_pass_by_value)] #[cfg_attr(#gate_rustc_only_cfg, rustc_pass_by_value)]
#vis struct #name { #vis struct #name {
private: u32, private_use_as_methods_instead: u32,
} }
#(#consts)* #(#consts)*
@ -238,7 +238,7 @@ impl Parse for Newtype {
/// Prefer using `from_u32`. /// Prefer using `from_u32`.
#[inline] #[inline]
#vis const unsafe fn from_u32_unchecked(value: u32) -> Self { #vis const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self { private: value } Self { private_use_as_methods_instead: value }
} }
/// Extracts the value of this index as a `usize`. /// Extracts the value of this index as a `usize`.
@ -250,7 +250,7 @@ impl Parse for Newtype {
/// Extracts the value of this index as a `u32`. /// Extracts the value of this index as a `u32`.
#[inline] #[inline]
#vis const fn as_u32(self) -> u32 { #vis const fn as_u32(self) -> u32 {
self.private self.private_use_as_methods_instead
} }
/// Extracts the value of this index as a `usize`. /// Extracts the value of this index as a `usize`.

View file

@ -327,21 +327,21 @@ impl UniverseIndex {
/// name the region `'a`, but that region was not nameable from /// name the region `'a`, but that region was not nameable from
/// `U` because it was not in scope there. /// `U` because it was not in scope there.
pub fn next_universe(self) -> UniverseIndex { pub fn next_universe(self) -> UniverseIndex {
UniverseIndex::from_u32(self.private.checked_add(1).unwrap()) UniverseIndex::from_u32(self.as_u32().checked_add(1).unwrap())
} }
/// Returns `true` if `self` can name a name from `other` -- in other words, /// Returns `true` if `self` can name a name from `other` -- in other words,
/// if the set of names in `self` is a superset of those in /// if the set of names in `self` is a superset of those in
/// `other` (`self >= other`). /// `other` (`self >= other`).
pub fn can_name(self, other: UniverseIndex) -> bool { pub fn can_name(self, other: UniverseIndex) -> bool {
self.private >= other.private self >= other
} }
/// Returns `true` if `self` cannot name some names from `other` -- in other /// Returns `true` if `self` cannot name some names from `other` -- in other
/// words, if the set of names in `self` is a strict subset of /// words, if the set of names in `self` is a strict subset of
/// those in `other` (`self < other`). /// those in `other` (`self < other`).
pub fn cannot_name(self, other: UniverseIndex) -> bool { pub fn cannot_name(self, other: UniverseIndex) -> bool {
self.private < other.private self < other
} }
} }