Skip to content

Commit 42cbfd6

Browse files
committed
yeet
1 parent d1206f9 commit 42cbfd6

File tree

9 files changed

+131
-57
lines changed

9 files changed

+131
-57
lines changed

compiler/rustc_middle/src/query/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1632,4 +1632,14 @@ rustc_queries! {
16321632
query normalize_opaque_types(key: &'tcx ty::List<ty::Predicate<'tcx>>) -> &'tcx ty::List<ty::Predicate<'tcx>> {
16331633
desc { "normalizing opaque types in {:?}", key }
16341634
}
1635+
1636+
/// Checks whether a type is definitely uninhabited. This is
1637+
/// conservative: for some types that are uninhabited we return `false`,
1638+
/// but we only return `true` for types that are definitely uninhabited.
1639+
/// `ty.conservative_is_privately_uninhabited` implies that any value of type `ty`
1640+
/// will be `Abi::Uninhabited`. (Note that uninhabited types may have nonzero
1641+
/// size, to account for partial initialisation. See #49298 for details.)
1642+
query conservative_is_privately_uninhabited(key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
1643+
desc { "conservatively checking if {:?} is privately uninhabited", key }
1644+
}
16351645
}

compiler/rustc_middle/src/ty/layout.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn layout_raw<'tcx>(
231231
let layout = cx.layout_raw_uncached(ty);
232232
// Type-level uninhabitedness should always imply ABI uninhabitedness.
233233
if let Ok(layout) = layout {
234-
if ty.conservative_is_privately_uninhabited(tcx) {
234+
if tcx.conservative_is_privately_uninhabited(param_env.and(ty)) {
235235
assert!(layout.abi.is_uninhabited());
236236
}
237237
}
@@ -583,11 +583,12 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
583583
let size =
584584
element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow(ty))?;
585585

586-
let abi = if count != 0 && ty.conservative_is_privately_uninhabited(tcx) {
587-
Abi::Uninhabited
588-
} else {
589-
Abi::Aggregate { sized: true }
590-
};
586+
let abi =
587+
if count != 0 && tcx.conservative_is_privately_uninhabited(param_env.and(ty)) {
588+
Abi::Uninhabited
589+
} else {
590+
Abi::Aggregate { sized: true }
591+
};
591592

592593
let largest_niche = if count != 0 { element.largest_niche.clone() } else { None };
593594

compiler/rustc_middle/src/ty/sty.rs

-47
Original file line numberDiff line numberDiff line change
@@ -1685,53 +1685,6 @@ impl<'tcx> TyS<'tcx> {
16851685
matches!(self.kind(), Never)
16861686
}
16871687

1688-
/// Checks whether a type is definitely uninhabited. This is
1689-
/// conservative: for some types that are uninhabited we return `false`,
1690-
/// but we only return `true` for types that are definitely uninhabited.
1691-
/// `ty.conservative_is_privately_uninhabited` implies that any value of type `ty`
1692-
/// will be `Abi::Uninhabited`. (Note that uninhabited types may have nonzero
1693-
/// size, to account for partial initialisation. See #49298 for details.)
1694-
pub fn conservative_is_privately_uninhabited(&self, tcx: TyCtxt<'tcx>) -> bool {
1695-
// FIXME(varkor): we can make this less conversative by substituting concrete
1696-
// type arguments.
1697-
match self.kind() {
1698-
ty::Never => true,
1699-
ty::Adt(def, _) if def.is_union() => {
1700-
// For now, `union`s are never considered uninhabited.
1701-
false
1702-
}
1703-
ty::Adt(def, _) => {
1704-
// Any ADT is uninhabited if either:
1705-
// (a) It has no variants (i.e. an empty `enum`);
1706-
// (b) Each of its variants (a single one in the case of a `struct`) has at least
1707-
// one uninhabited field.
1708-
def.variants.iter().all(|var| {
1709-
var.fields.iter().any(|field| {
1710-
tcx.type_of(field.did).conservative_is_privately_uninhabited(tcx)
1711-
})
1712-
})
1713-
}
1714-
ty::Tuple(..) => {
1715-
self.tuple_fields().any(|ty| ty.conservative_is_privately_uninhabited(tcx))
1716-
}
1717-
ty::Array(ty, len) => {
1718-
match len.try_eval_usize(tcx, ParamEnv::empty()) {
1719-
Some(0) | None => false,
1720-
// If the array is definitely non-empty, it's uninhabited if
1721-
// the type of its elements is uninhabited.
1722-
Some(1..) => ty.conservative_is_privately_uninhabited(tcx),
1723-
}
1724-
}
1725-
ty::Ref(..) => {
1726-
// References to uninitialised memory is valid for any type, including
1727-
// uninhabited types, in unsafe code, so we treat all references as
1728-
// inhabited.
1729-
false
1730-
}
1731-
_ => false,
1732-
}
1733-
}
1734-
17351688
#[inline]
17361689
pub fn is_primitive(&self) -> bool {
17371690
self.kind().is_primitive()

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17301730
}
17311731
}
17321732
None => {
1733-
if !sig.output().conservative_is_privately_uninhabited(self.tcx()) {
1733+
if !self
1734+
.tcx()
1735+
.conservative_is_privately_uninhabited(self.param_env.and(sig.output()))
1736+
{
17341737
span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
17351738
}
17361739
}

compiler/rustc_mir/src/transform/generator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,9 @@ fn insert_panic_block<'tcx>(
10071007
assert_block
10081008
}
10091009

1010-
fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
1010+
fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
10111011
// Returning from a function with an uninhabited return type is undefined behavior.
1012-
if body.return_ty().conservative_is_privately_uninhabited(tcx) {
1012+
if tcx.conservative_is_privately_uninhabited(param_env.and(body.return_ty())) {
10131013
return false;
10141014
}
10151015

@@ -1320,7 +1320,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
13201320
// `storage_liveness` tells us which locals have live storage at suspension points
13211321
let (remap, layout, storage_liveness) = compute_layout(liveness_info, body);
13221322

1323-
let can_return = can_return(tcx, body);
1323+
let can_return = can_return(tcx, body, tcx.param_env(body.source.def_id()));
13241324

13251325
// Run the transformation which converts Places from Local to generator struct
13261326
// accesses for locals in `remap`.

compiler/rustc_ty_utils/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! This API is completely unstable and subject to change.
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8+
#![feature(half_open_range_patterns)]
9+
#![feature(exclusive_range_pattern)]
810
#![feature(nll)]
911
#![recursion_limit = "256"]
1012

compiler/rustc_ty_utils/src/ty.rs

+58
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,63 @@ fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
484484
fn_like.asyncness()
485485
}
486486

487+
/// Don't call this directly: use ``tcx.conservative_is_privately_uninhabited`` instead.
488+
#[instrument(level = "debug", skip(tcx))]
489+
pub fn conservative_is_privately_uninhabited_raw<'tcx>(
490+
tcx: TyCtxt<'tcx>,
491+
param_env_and: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
492+
) -> bool {
493+
let (param_env, ty) = param_env_and.into_parts();
494+
match ty.kind() {
495+
ty::Never => {
496+
debug!("ty::Never =>");
497+
true
498+
}
499+
ty::Adt(def, _) if def.is_union() => {
500+
debug!("ty::Adt(def, _) if def.is_union() =>");
501+
// For now, `union`s are never considered uninhabited.
502+
false
503+
}
504+
ty::Adt(def, substs) => {
505+
debug!("ty::Adt(def, _) if def.is_not_union() =>");
506+
// Any ADT is uninhabited if either:
507+
// (a) It has no variants (i.e. an empty `enum`);
508+
// (b) Each of its variants (a single one in the case of a `struct`) has at least
509+
// one uninhabited field.
510+
def.variants.iter().all(|var| {
511+
var.fields.iter().any(|field| {
512+
let ty = tcx.type_of(field.did).subst(tcx, substs);
513+
tcx.conservative_is_privately_uninhabited(param_env.and(ty))
514+
})
515+
})
516+
}
517+
ty::Tuple(..) => {
518+
debug!("ty::Tuple(..) =>");
519+
ty.tuple_fields().any(|ty| tcx.conservative_is_privately_uninhabited(param_env.and(ty)))
520+
}
521+
ty::Array(ty, len) => {
522+
debug!("ty::Array(ty, len) =>");
523+
match len.try_eval_usize(tcx, param_env) {
524+
Some(0) | None => false,
525+
// If the array is definitely non-empty, it's uninhabited if
526+
// the type of its elements is uninhabited.
527+
Some(1..) => tcx.conservative_is_privately_uninhabited(param_env.and(ty)),
528+
}
529+
}
530+
ty::Ref(..) => {
531+
debug!("ty::Ref(..) =>");
532+
// References to uninitialised memory is valid for any type, including
533+
// uninhabited types, in unsafe code, so we treat all references as
534+
// inhabited.
535+
false
536+
}
537+
_ => {
538+
debug!("_ =>");
539+
false
540+
}
541+
}
542+
}
543+
487544
pub fn provide(providers: &mut ty::query::Providers) {
488545
*providers = ty::query::Providers {
489546
asyncness,
@@ -501,6 +558,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
501558
instance_def_size_estimate,
502559
issue33140_self_ty,
503560
impl_defaultness,
561+
conservative_is_privately_uninhabited: conservative_is_privately_uninhabited_raw,
504562
..*providers
505563
};
506564
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// run-pass
2+
#![feature(const_generics, const_evaluatable_checked)]
3+
#![allow(incomplete_features)]
4+
5+
// This tests that the `conservative_is_privately_uninhabited` fn doesn't cause
6+
// ICEs by trying to evaluate `T::ASSOC` with an incorrect `ParamEnv`.
7+
8+
trait Foo {
9+
const ASSOC: usize = 1;
10+
}
11+
12+
struct Iced<T: Foo>(T, [(); T::ASSOC])
13+
where
14+
[(); T::ASSOC]: ;
15+
16+
impl Foo for u32 {}
17+
18+
fn foo<T: Foo>()
19+
where
20+
[(); T::ASSOC]: ,
21+
{
22+
let _iced: Iced<T> = return;
23+
}
24+
25+
fn main() {
26+
foo::<u32>();
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-pass
2+
#![feature(const_generics, const_evaluatable_checked)]
3+
#![allow(incomplete_features)]
4+
5+
// This tests that the `conservative_is_privately_uninhabited` fn doesn't cause
6+
// ICEs by trying to evaluate `T::ASSOC` with an incorrect `ParamEnv`.
7+
8+
trait Foo {
9+
const ASSOC: usize = 1;
10+
}
11+
12+
struct Iced<T: Foo>(T, [(); T::ASSOC])
13+
where
14+
[(); T::ASSOC]: ;
15+
16+
impl Foo for u32 {}
17+
18+
fn main() {
19+
let _iced: Iced<u32> = return;
20+
}

0 commit comments

Comments
 (0)