From 4d20dd4b788a9e825bac43c8c0b0b4858de78296 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 5 Nov 2018 08:37:00 -0700 Subject: [PATCH] Fix emission of niche-filling discriminant values Bug #55606 points out a regression introduced by #54004; namely that an assertion can erroneously fire when a niche-filling discriminant value is emitted. This fixes the bug by removing the assertion, and furthermore by arranging for the discriminant value to be masked according to the size of the niche. This makes handling the discriminant a bit simpler for debuggers. The test case is from Jonathan Turner. Closes #55606 --- .../debuginfo/metadata.rs | 8 +-- src/test/codegen/enum-debug-niche-2.rs | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/test/codegen/enum-debug-niche-2.rs diff --git a/src/librustc_codegen_llvm/debuginfo/metadata.rs b/src/librustc_codegen_llvm/debuginfo/metadata.rs index ba1e3f5960c85..6c70aa23dc2e6 100644 --- a/src/librustc_codegen_llvm/debuginfo/metadata.rs +++ b/src/librustc_codegen_llvm/debuginfo/metadata.rs @@ -1279,7 +1279,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { niche_start, ref variants, dataful_variant, - .. + ref niche, } => { if fallback { let variant = self.layout.for_variant(cx, dataful_variant); @@ -1361,11 +1361,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { let niche_value = if i == dataful_variant { None } else { - let niche = (i as u128) + let value = (i as u128) .wrapping_sub(*niche_variants.start() as u128) .wrapping_add(niche_start); - assert_eq!(niche as u64 as u128, niche); - Some(niche as u64) + let value = value & ((1u128 << niche.value.size(cx).bits()) - 1); + Some(value as u64) }; MemberDescription { diff --git a/src/test/codegen/enum-debug-niche-2.rs b/src/test/codegen/enum-debug-niche-2.rs new file mode 100644 index 0000000000000..c0ee05110fcdc --- /dev/null +++ b/src/test/codegen/enum-debug-niche-2.rs @@ -0,0 +1,63 @@ +// Copyright 2018 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This test depends on a patch that was committed to upstream LLVM +// before 7.0, then backported to the Rust LLVM fork. It tests that +// optimized enum debug info accurately reflects the enum layout. + +// ignore-tidy-linelength +// ignore-windows +// min-system-llvm-version 7.0 + +// compile-flags: -g -C no-prepopulate-passes + +// CHECK: {{.*}}DICompositeType{{.*}}tag: DW_TAG_variant_part,{{.*}}size: 32,{{.*}} +// CHECK: {{.*}}DIDerivedType{{.*}}tag: DW_TAG_member,{{.*}}name: "Placeholder",{{.*}}extraData: i64 4294967295{{[,)].*}} +// CHECK: {{.*}}DIDerivedType{{.*}}tag: DW_TAG_member,{{.*}}name: "Error",{{.*}}extraData: i64 0{{[,)].*}} + +#![feature(never_type)] +#![feature(nll)] + +#[derive(Copy, Clone)] +pub struct Entity { + private: std::num::NonZeroU32, +} + +#[derive(Copy, Clone, PartialEq, Eq)] +pub struct Declaration; + +impl TypeFamily for Declaration { + type Base = Base; + type Placeholder = !; + + fn intern_base_data(_: BaseKind) {} +} + +#[derive(Copy, Clone)] +pub struct Base; + +pub trait TypeFamily: Copy + 'static { + type Base: Copy; + type Placeholder: Copy; + + fn intern_base_data(_: BaseKind); +} + +#[derive(Copy, Clone)] +pub enum BaseKind { + Named(Entity), + Placeholder(F::Placeholder), + Error, +} + +pub fn main() { + let x = BaseKind::Error::; + let y = 7; +}