Skip to content

Commit 25d6f8e

Browse files
committed
Avoid looking at the internals of Interned directly
1 parent 2e0ef70 commit 25d6f8e

File tree

7 files changed

+150
-199
lines changed

7 files changed

+150
-199
lines changed

compiler/rustc_infer/src/infer/outlives/env.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::infer::free_regions::FreeRegionMap;
22
use crate::infer::{GenericKind, InferCtxt};
33
use crate::traits::query::OutlivesBound;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_data_structures::intern::Interned;
65
use rustc_hir as hir;
76
use rustc_middle::ty::{self, ReEarlyBound, ReFree, ReVar, Region};
87

@@ -164,12 +163,6 @@ impl<'a, 'tcx> OutlivesEnvironment<'tcx> {
164163
for outlives_bound in outlives_bounds {
165164
debug!("add_outlives_bounds: outlives_bound={:?}", outlives_bound);
166165
match outlives_bound {
167-
OutlivesBound::RegionSubRegion(
168-
r_a @ (Region(Interned(ReEarlyBound(_), _)) | Region(Interned(ReFree(_), _))),
169-
Region(Interned(ReVar(vid_b), _)),
170-
) => {
171-
infcx.expect("no infcx provided but region vars found").add_given(r_a, *vid_b);
172-
}
173166
OutlivesBound::RegionSubParam(r_a, param_b) => {
174167
self.region_bound_pairs_accum.push((r_a, GenericKind::Param(param_b)));
175168
}
@@ -178,17 +171,23 @@ impl<'a, 'tcx> OutlivesEnvironment<'tcx> {
178171
.push((r_a, GenericKind::Projection(projection_b)));
179172
}
180173
OutlivesBound::RegionSubRegion(r_a, r_b) => {
181-
// In principle, we could record (and take
182-
// advantage of) every relationship here, but
183-
// we are also free not to -- it simply means
184-
// strictly less that we can successfully type
185-
// check. Right now we only look for things
186-
// relationships between free regions. (It may
187-
// also be that we should revise our inference
188-
// system to be more general and to make use
189-
// of *every* relationship that arises here,
190-
// but presently we do not.)
191-
self.free_region_map.relate_regions(r_a, r_b);
174+
if let (ReEarlyBound(_) | ReFree(_), ReVar(vid_b)) = (r_a.kind(), r_b.kind()) {
175+
infcx
176+
.expect("no infcx provided but region vars found")
177+
.add_given(r_a, vid_b);
178+
} else {
179+
// In principle, we could record (and take
180+
// advantage of) every relationship here, but
181+
// we are also free not to -- it simply means
182+
// strictly less that we can successfully type
183+
// check. Right now we only look for things
184+
// relationships between free regions. (It may
185+
// also be that we should revise our inference
186+
// system to be more general and to make use
187+
// of *every* relationship that arises here,
188+
// but presently we do not.)
189+
self.free_region_map.relate_regions(r_a, r_b);
190+
}
192191
}
193192
}
194193
}

compiler/rustc_middle/src/ty/layout.rs

+28-40
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::ty::subst::Subst;
55
use crate::ty::{self, subst::SubstsRef, ReprOptions, Ty, TyCtxt, TypeFoldable};
66
use rustc_ast as ast;
77
use rustc_attr as attr;
8-
use rustc_data_structures::intern::Interned;
98
use rustc_hir as hir;
109
use rustc_hir::lang_items::LangItem;
1110
use rustc_index::bit_set::BitSet;
@@ -503,42 +502,34 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
503502
}
504503

505504
// Two non-ZST fields, and they're both scalars.
506-
(
507-
Some((
508-
i,
509-
&TyAndLayout {
510-
layout: Layout(Interned(&LayoutS { abi: Abi::Scalar(a), .. }, _)),
511-
..
512-
},
513-
)),
514-
Some((
515-
j,
516-
&TyAndLayout {
517-
layout: Layout(Interned(&LayoutS { abi: Abi::Scalar(b), .. }, _)),
518-
..
519-
},
520-
)),
521-
None,
522-
) => {
523-
// Order by the memory placement, not source order.
524-
let ((i, a), (j, b)) =
525-
if offsets[i] < offsets[j] { ((i, a), (j, b)) } else { ((j, b), (i, a)) };
526-
let pair = self.scalar_pair(a, b);
527-
let pair_offsets = match pair.fields {
528-
FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
529-
assert_eq!(memory_index, &[0, 1]);
530-
offsets
505+
(Some((i, a)), Some((j, b)), None) => {
506+
match (a.abi, b.abi) {
507+
(Abi::Scalar(a), Abi::Scalar(b)) => {
508+
// Order by the memory placement, not source order.
509+
let ((i, a), (j, b)) = if offsets[i] < offsets[j] {
510+
((i, a), (j, b))
511+
} else {
512+
((j, b), (i, a))
513+
};
514+
let pair = self.scalar_pair(a, b);
515+
let pair_offsets = match pair.fields {
516+
FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
517+
assert_eq!(memory_index, &[0, 1]);
518+
offsets
519+
}
520+
_ => bug!(),
521+
};
522+
if offsets[i] == pair_offsets[0]
523+
&& offsets[j] == pair_offsets[1]
524+
&& align == pair.align
525+
&& size == pair.size
526+
{
527+
// We can use `ScalarPair` only when it matches our
528+
// already computed layout (including `#[repr(C)]`).
529+
abi = pair.abi;
530+
}
531531
}
532-
_ => bug!(),
533-
};
534-
if offsets[i] == pair_offsets[0]
535-
&& offsets[j] == pair_offsets[1]
536-
&& align == pair.align
537-
&& size == pair.size
538-
{
539-
// We can use `ScalarPair` only when it matches our
540-
// already computed layout (including `#[repr(C)]`).
541-
abi = pair.abi;
532+
_ => {}
542533
}
543534
}
544535

@@ -788,10 +779,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
788779
}
789780

790781
// Extract the number of elements from the layout of the array field:
791-
let Ok(TyAndLayout {
792-
layout: Layout(Interned(LayoutS { fields: FieldsShape::Array { count, .. }, .. }, _)),
793-
..
794-
}) = self.layout_of(f0_ty) else {
782+
let FieldsShape::Array { count, .. } = self.layout_of(f0_ty)?.layout.fields() else {
795783
return Err(LayoutError::Unknown(ty));
796784
};
797785

compiler/rustc_middle/src/ty/print/pretty.rs

+76-102
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
33
use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, Ty, TyCtxt, TypeFoldable};
44
use rustc_apfloat::ieee::{Double, Single};
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_data_structures::intern::{Interned, WithStableHash};
76
use rustc_data_structures::sso::SsoHashSet;
87
use rustc_hir as hir;
98
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
@@ -1263,71 +1262,52 @@ pub trait PrettyPrinter<'tcx>:
12631262
let (alloc_id, offset) = ptr.into_parts();
12641263
match ty.kind() {
12651264
// Byte strings (&[u8; N])
1266-
ty::Ref(
1267-
_,
1268-
Ty(Interned(
1269-
WithStableHash {
1270-
internee:
1271-
ty::TyS {
1272-
kind:
1273-
ty::Array(
1274-
Ty(Interned(
1275-
WithStableHash {
1276-
internee:
1277-
ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. },
1278-
..
1279-
},
1280-
_,
1281-
)),
1282-
ty::Const(Interned(
1283-
ty::ConstS {
1284-
val: ty::ConstKind::Value(ConstValue::Scalar(int)),
1285-
..
1286-
},
1287-
_,
1288-
)),
1289-
),
1290-
..
1291-
},
1292-
..
1293-
},
1294-
_,
1295-
)),
1296-
_,
1297-
) => match self.tcx().get_global_alloc(alloc_id) {
1298-
Some(GlobalAlloc::Memory(alloc)) => {
1299-
let len = int.assert_bits(self.tcx().data_layout.pointer_size);
1300-
let range = AllocRange { start: offset, size: Size::from_bytes(len) };
1301-
if let Ok(byte_str) = alloc.inner().get_bytes(&self.tcx(), range) {
1302-
p!(pretty_print_byte_str(byte_str))
1303-
} else {
1304-
p!("<too short allocation>")
1265+
ty::Ref(_, inner, _) => {
1266+
if let ty::Array(elem, len) = inner.kind() {
1267+
if let ty::Uint(ty::UintTy::U8) = elem.kind() {
1268+
if let ty::ConstKind::Value(ConstValue::Scalar(int)) = len.val() {
1269+
match self.tcx().get_global_alloc(alloc_id) {
1270+
Some(GlobalAlloc::Memory(alloc)) => {
1271+
let len = int.assert_bits(self.tcx().data_layout.pointer_size);
1272+
let range =
1273+
AllocRange { start: offset, size: Size::from_bytes(len) };
1274+
if let Ok(byte_str) =
1275+
alloc.inner().get_bytes(&self.tcx(), range)
1276+
{
1277+
p!(pretty_print_byte_str(byte_str))
1278+
} else {
1279+
p!("<too short allocation>")
1280+
}
1281+
}
1282+
// FIXME: for statics and functions, we could in principle print more detail.
1283+
Some(GlobalAlloc::Static(def_id)) => {
1284+
p!(write("<static({:?})>", def_id))
1285+
}
1286+
Some(GlobalAlloc::Function(_)) => p!("<function>"),
1287+
None => p!("<dangling pointer>"),
1288+
}
1289+
return Ok(self);
1290+
}
13051291
}
13061292
}
1307-
// FIXME: for statics and functions, we could in principle print more detail.
1308-
Some(GlobalAlloc::Static(def_id)) => p!(write("<static({:?})>", def_id)),
1309-
Some(GlobalAlloc::Function(_)) => p!("<function>"),
1310-
None => p!("<dangling pointer>"),
1311-
},
1293+
}
13121294
ty::FnPtr(_) => {
13131295
// FIXME: We should probably have a helper method to share code with the "Byte strings"
13141296
// printing above (which also has to handle pointers to all sorts of things).
1315-
match self.tcx().get_global_alloc(alloc_id) {
1316-
Some(GlobalAlloc::Function(instance)) => {
1317-
self = self.typed_value(
1318-
|this| this.print_value_path(instance.def_id(), instance.substs),
1319-
|this| this.print_type(ty),
1320-
" as ",
1321-
)?;
1322-
}
1323-
_ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?,
1297+
if let Some(GlobalAlloc::Function(instance)) = self.tcx().get_global_alloc(alloc_id)
1298+
{
1299+
self = self.typed_value(
1300+
|this| this.print_value_path(instance.def_id(), instance.substs),
1301+
|this| this.print_type(ty),
1302+
" as ",
1303+
)?;
1304+
return Ok(self);
13241305
}
13251306
}
1326-
// Any pointer values not covered by a branch above
1327-
_ => {
1328-
self = self.pretty_print_const_pointer(ptr, ty, print_ty)?;
1329-
}
1307+
_ => {}
13301308
}
1309+
// Any pointer values not covered by a branch above
1310+
self = self.pretty_print_const_pointer(ptr, ty, print_ty)?;
13311311
Ok(self)
13321312
}
13331313

@@ -1448,39 +1428,31 @@ pub trait PrettyPrinter<'tcx>:
14481428

14491429
match (ct, ty.kind()) {
14501430
// Byte/string slices, printed as (byte) string literals.
1451-
(
1452-
ConstValue::Slice { data, start, end },
1453-
ty::Ref(
1454-
_,
1455-
Ty(Interned(
1456-
WithStableHash { internee: ty::TyS { kind: ty::Slice(t), .. }, .. },
1457-
_,
1458-
)),
1459-
_,
1460-
),
1461-
) if *t == u8_type => {
1462-
// The `inspect` here is okay since we checked the bounds, and there are
1463-
// no relocations (we have an active slice reference here). We don't use
1464-
// this result to affect interpreter execution.
1465-
let byte_str =
1466-
data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end);
1467-
self.pretty_print_byte_str(byte_str)
1468-
}
1469-
(
1470-
ConstValue::Slice { data, start, end },
1471-
ty::Ref(
1472-
_,
1473-
Ty(Interned(WithStableHash { internee: ty::TyS { kind: ty::Str, .. }, .. }, _)),
1474-
_,
1475-
),
1476-
) => {
1477-
// The `inspect` here is okay since we checked the bounds, and there are no
1478-
// relocations (we have an active `str` reference here). We don't use this
1479-
// result to affect interpreter execution.
1480-
let slice =
1481-
data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end);
1482-
p!(write("{:?}", String::from_utf8_lossy(slice)));
1483-
Ok(self)
1431+
(ConstValue::Slice { data, start, end }, ty::Ref(_, inner, _)) => {
1432+
match inner.kind() {
1433+
ty::Slice(t) => {
1434+
if *t == u8_type {
1435+
// The `inspect` here is okay since we checked the bounds, and there are
1436+
// no relocations (we have an active slice reference here). We don't use
1437+
// this result to affect interpreter execution.
1438+
let byte_str = data
1439+
.inner()
1440+
.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
1441+
return self.pretty_print_byte_str(byte_str);
1442+
}
1443+
}
1444+
ty::Str => {
1445+
// The `inspect` here is okay since we checked the bounds, and there are no
1446+
// relocations (we have an active `str` reference here). We don't use this
1447+
// result to affect interpreter execution.
1448+
let slice = data
1449+
.inner()
1450+
.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
1451+
p!(write("{:?}", String::from_utf8_lossy(slice)));
1452+
return Ok(self);
1453+
}
1454+
_ => {}
1455+
}
14841456
}
14851457
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
14861458
let n = n.val().try_to_bits(self.tcx().data_layout.pointer_size).unwrap();
@@ -1490,7 +1462,7 @@ pub trait PrettyPrinter<'tcx>:
14901462
let byte_str = alloc.inner().get_bytes(&self.tcx(), range).unwrap();
14911463
p!("*");
14921464
p!(pretty_print_byte_str(byte_str));
1493-
Ok(self)
1465+
return Ok(self);
14941466
}
14951467

14961468
// Aggregates, printed as array/tuple/struct/variant construction syntax.
@@ -1567,22 +1539,24 @@ pub trait PrettyPrinter<'tcx>:
15671539
_ => unreachable!(),
15681540
}
15691541

1570-
Ok(self)
1542+
return Ok(self);
15711543
}
15721544

1573-
(ConstValue::Scalar(scalar), _) => self.pretty_print_const_scalar(scalar, ty, print_ty),
1545+
(ConstValue::Scalar(scalar), _) => {
1546+
return self.pretty_print_const_scalar(scalar, ty, print_ty);
1547+
}
15741548

15751549
// FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
15761550
// their fields instead of just dumping the memory.
1577-
_ => {
1578-
// fallback
1579-
p!(write("{:?}", ct));
1580-
if print_ty {
1581-
p!(": ", print(ty));
1582-
}
1583-
Ok(self)
1584-
}
1551+
_ => {}
15851552
}
1553+
1554+
// fallback
1555+
p!(write("{:?}", ct));
1556+
if print_ty {
1557+
p!(": ", print(ty));
1558+
}
1559+
Ok(self)
15861560
}
15871561
}
15881562

0 commit comments

Comments
 (0)