Skip to content

Commit 76192f0

Browse files
committed
Auto merge of rust-lang#123169 - lukas-code:known-panics-ice, r=<try>
skip known panics lint for impossible items fixes rust-lang#123134 For items with impossible predicates like `[u8]: Sized` it's possible to have locals that are "Sized", but cannot be const-propped in any meaningful way. To avoid this issue, we can just skip the known panics lint for items that have impossible predicates.
2 parents 4ea92e3 + 7b46e02 commit 76192f0

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

compiler/rustc_middle/src/query/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,13 @@ rustc_queries! {
21162116
}
21172117
}
21182118

2119+
query is_impossible_item(def_id: DefId) -> bool {
2120+
desc { |tcx|
2121+
"checking if `{}` is impossible to reference",
2122+
tcx.def_path_str(def_id),
2123+
}
2124+
}
2125+
21192126
query is_impossible_associated_item(key: (DefId, DefId)) -> bool {
21202127
desc { |tcx|
21212128
"checking if `{}` is impossible to reference within `{}`",

compiler/rustc_mir_transform/src/known_panics_lint.rs

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ impl<'tcx> MirLint<'tcx> for KnownPanicsLint {
5151
return;
5252
}
5353

54+
if tcx.is_impossible_item(def_id) {
55+
trace!("KnownPanicsLint skipped for impossible item {:?}", def_id);
56+
return;
57+
}
58+
5459
trace!("KnownPanicsLint starting for {:?}", def_id);
5560

5661
let mut linter = ConstPropagator::new(body, tcx);

compiler/rustc_trait_selection/src/traits/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ fn instantiate_and_check_impossible_predicates<'tcx>(
421421
result
422422
}
423423

424+
/// Checks whether an item is impossible to reference.
425+
#[instrument(level = "debug", skip(tcx), ret)]
426+
fn is_impossible_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
427+
let param_env = tcx.param_env_reveal_all_normalized(def_id);
428+
impossible_predicates(
429+
tcx,
430+
param_env.caller_bounds().iter().filter(|predicate| !predicate.has_param()).collect(),
431+
)
432+
}
433+
424434
/// Checks whether a trait's associated item is impossible to reference on a given impl.
425435
///
426436
/// This only considers predicates that reference the impl's generics, and not
@@ -506,6 +516,7 @@ pub fn provide(providers: &mut Providers) {
506516
specializes: specialize::specializes,
507517
instantiate_and_check_impossible_predicates,
508518
check_tys_might_be_eq: misc::check_tys_might_be_eq,
519+
is_impossible_item,
509520
is_impossible_associated_item,
510521
..*providers
511522
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@ build-pass
2+
// Regression test for an ICE: https://github.com/rust-lang/rust/issues/123134
3+
4+
trait Api: Sized {
5+
type Device: ?Sized;
6+
}
7+
8+
struct OpenDevice<A: Api>
9+
where
10+
A::Device: Sized,
11+
{
12+
device: A::Device,
13+
queue: (),
14+
}
15+
16+
trait Adapter {
17+
type A: Api;
18+
19+
fn open() -> OpenDevice<Self::A>
20+
where
21+
<Self::A as Api>::Device: Sized;
22+
}
23+
24+
struct ApiS;
25+
26+
impl Api for ApiS {
27+
type Device = [u8];
28+
}
29+
30+
impl<T> Adapter for T
31+
{
32+
type A = ApiS;
33+
34+
// This function has the impossible predicate `[u8]: Sized`.
35+
fn open() -> OpenDevice<Self::A>
36+
where
37+
<Self::A as Api>::Device: Sized,
38+
{
39+
unreachable!()
40+
}
41+
}
42+
43+
fn main() {}

0 commit comments

Comments
 (0)