Skip to content

Commit

Permalink
rustc_trans: don't write discriminants for uninhabited variants
Browse files Browse the repository at this point in the history
Fixes rust-lang#46519.

Patch as suggested by eddyb:
rust-lang#46519 (comment)
  • Loading branch information
SimonSapin authored and michaelwoerister committed Dec 7, 2017
1 parent 9b2878c commit 4e28c61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/librustc_trans/mir/lvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,12 @@ impl<'a, 'tcx> LvalueRef<'tcx> {
/// Set the discriminant for a new value of the given case of the given
/// representation.
pub fn trans_set_discr(&self, bcx: &Builder<'a, 'tcx>, variant_index: usize) {
match self.layout.variants {
if self.layout.for_variant(bcx.ccx, variant_index).abi == layout::Abi::Uninhabited {
return;
}
match self.layout.variants {
layout::Variants::Single { index } => {
if index != variant_index {
// If the layout of an enum is `Single`, all
// other variants are necessarily uninhabited.
assert_eq!(self.layout.for_variant(bcx.ccx, variant_index).abi,
layout::Abi::Uninhabited);
}
assert_eq!(index, variant_index);
}
layout::Variants::Tagged { .. } => {
let ptr = self.project_field(bcx, 0);
Expand Down
37 changes: 37 additions & 0 deletions src/test/run-pass/issue-46519.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags:--test -O

#[test]
#[should_panic(expected = "creating inhabited type")]
fn test() {
FontLanguageOverride::system_font(SystemFont::new());
}

pub enum FontLanguageOverride {
Normal,
Override(&'static str),
System(SystemFont)
}

pub enum SystemFont {}

impl FontLanguageOverride {
fn system_font(f: SystemFont) -> Self {
FontLanguageOverride::System(f)
}
}

impl SystemFont {
fn new() -> Self {
panic!("creating inhabited type")
}
}

0 comments on commit 4e28c61

Please sign in to comment.