Skip to content

Commit a62910b

Browse files
committed
Auto merge of #46521 - SimonSapin:uninhabited-variants, r=eddyb
rustc_trans: don't write discriminants for uninhabited variants Fixes #46519. Patch as suggested by eddyb: #46519 (comment)
2 parents 6a5895c + d4fabb9 commit a62910b

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/librustc_trans/mir/place.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,12 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
359359
/// Set the discriminant for a new value of the given case of the given
360360
/// representation.
361361
pub fn trans_set_discr(&self, bcx: &Builder<'a, 'tcx>, variant_index: usize) {
362-
match self.layout.variants {
362+
if self.layout.for_variant(bcx.ccx, variant_index).abi == layout::Abi::Uninhabited {
363+
return;
364+
}
365+
match self.layout.variants {
363366
layout::Variants::Single { index } => {
364-
if index != variant_index {
365-
// If the layout of an enum is `Single`, all
366-
// other variants are necessarily uninhabited.
367-
assert_eq!(self.layout.for_variant(bcx.ccx, variant_index).abi,
368-
layout::Abi::Uninhabited);
369-
}
367+
assert_eq!(index, variant_index);
370368
}
371369
layout::Variants::Tagged { .. } => {
372370
let ptr = self.project_field(bcx, 0);

src/test/run-pass/issue-46519.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:--test -O
12+
13+
#[test]
14+
#[should_panic(expected = "creating inhabited type")]
15+
fn test() {
16+
FontLanguageOverride::system_font(SystemFont::new());
17+
}
18+
19+
pub enum FontLanguageOverride {
20+
Normal,
21+
Override(&'static str),
22+
System(SystemFont)
23+
}
24+
25+
pub enum SystemFont {}
26+
27+
impl FontLanguageOverride {
28+
fn system_font(f: SystemFont) -> Self {
29+
FontLanguageOverride::System(f)
30+
}
31+
}
32+
33+
impl SystemFont {
34+
fn new() -> Self {
35+
panic!("creating inhabited type")
36+
}
37+
}

0 commit comments

Comments
 (0)