Skip to content

Commit ee12697

Browse files
authored
Rollup merge of rust-lang#120134 - oli-obk:newtype_index_private_field, r=compiler-errors
Restrict access to the private field of newtype indexes Well... we don't have the capability to forbid you to access private fields in the same module, and I don't want to add module shenanigans in the expansion of the macro. So... we just name the field creatively so that no one actually uses it.
2 parents c851150 + 93740f9 commit ee12697

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

compiler/rustc_index_macros/src/newtype.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Parse for Newtype {
104104
#gate_rustc_only
105105
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for #name {
106106
fn encode(&self, e: &mut E) {
107-
e.emit_u32(self.private);
107+
e.emit_u32(self.as_u32());
108108
}
109109
}
110110
}
@@ -164,7 +164,7 @@ impl Parse for Newtype {
164164
#[inline]
165165
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
166166
if #max_val < u32::MAX {
167-
l.map(|i| i.private).unwrap_or(#max_val+1) == r.map(|i| i.private).unwrap_or(#max_val+1)
167+
l.map(|i| i.as_u32()).unwrap_or(#max_val+1) == r.map(|i| i.as_u32()).unwrap_or(#max_val+1)
168168
} else {
169169
match (l, r) {
170170
(Some(l), Some(r)) => r == l,
@@ -188,7 +188,7 @@ impl Parse for Newtype {
188188
#[cfg_attr(#gate_rustc_only_cfg, rustc_layout_scalar_valid_range_end(#max))]
189189
#[cfg_attr(#gate_rustc_only_cfg, rustc_pass_by_value)]
190190
#vis struct #name {
191-
private: u32,
191+
private_use_as_methods_instead: u32,
192192
}
193193

194194
#(#consts)*
@@ -238,7 +238,7 @@ impl Parse for Newtype {
238238
/// Prefer using `from_u32`.
239239
#[inline]
240240
#vis const unsafe fn from_u32_unchecked(value: u32) -> Self {
241-
Self { private: value }
241+
Self { private_use_as_methods_instead: value }
242242
}
243243

244244
/// Extracts the value of this index as a `usize`.
@@ -250,7 +250,7 @@ impl Parse for Newtype {
250250
/// Extracts the value of this index as a `u32`.
251251
#[inline]
252252
#vis const fn as_u32(self) -> u32 {
253-
self.private
253+
self.private_use_as_methods_instead
254254
}
255255

256256
/// Extracts the value of this index as a `usize`.

compiler/rustc_type_ir/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,21 @@ impl UniverseIndex {
327327
/// name the region `'a`, but that region was not nameable from
328328
/// `U` because it was not in scope there.
329329
pub fn next_universe(self) -> UniverseIndex {
330-
UniverseIndex::from_u32(self.private.checked_add(1).unwrap())
330+
UniverseIndex::from_u32(self.as_u32().checked_add(1).unwrap())
331331
}
332332

333333
/// Returns `true` if `self` can name a name from `other` -- in other words,
334334
/// if the set of names in `self` is a superset of those in
335335
/// `other` (`self >= other`).
336336
pub fn can_name(self, other: UniverseIndex) -> bool {
337-
self.private >= other.private
337+
self >= other
338338
}
339339

340340
/// Returns `true` if `self` cannot name some names from `other` -- in other
341341
/// words, if the set of names in `self` is a strict subset of
342342
/// those in `other` (`self < other`).
343343
pub fn cannot_name(self, other: UniverseIndex) -> bool {
344-
self.private < other.private
344+
self < other
345345
}
346346
}
347347

0 commit comments

Comments
 (0)