Skip to content

Commit

Permalink
Rollup merge of rust-lang#72600 - Aaron1011:fix/anon-const-encoding, …
Browse files Browse the repository at this point in the history
…r=varkor

Properly encode AnonConst into crate metadata

Fixes rust-lang#68104

Previous, we were encoding AnonConst as a regular Const, causing us to
treat them differently after being deserialized in another compilation
session.
  • Loading branch information
RalfJung authored Jun 20, 2020
2 parents 033013c + ebe5a91 commit 52e8dcb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ impl MetadataBlob {
impl EntryKind {
fn def_kind(&self) -> DefKind {
match *self {
EntryKind::AnonConst(..) => DefKind::AnonConst,
EntryKind::Const(..) => DefKind::Const,
EntryKind::AssocConst(..) => DefKind::AssocConst,
EntryKind::ImmStatic
Expand Down Expand Up @@ -1121,7 +1122,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn mir_const_qualif(&self, id: DefIndex) -> mir::ConstQualifs {
match self.kind(id) {
EntryKind::Const(qualif, _)
EntryKind::AnonConst(qualif, _)
| EntryKind::Const(qualif, _)
| EntryKind::AssocConst(
AssocContainer::ImplDefault
| AssocContainer::ImplFinal
Expand Down Expand Up @@ -1340,7 +1342,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn get_rendered_const(&self, id: DefIndex) -> String {
match self.kind(id) {
EntryKind::Const(_, data) | EntryKind::AssocConst(_, _, data) => data.decode(self).0,
EntryKind::AnonConst(_, data)
| EntryKind::Const(_, data)
| EntryKind::AssocConst(_, _, data) => data.decode(self).0,
_ => bug!(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ impl EncodeContext<'tcx> {
let const_data = self.encode_rendered_const_for_body(body_id);
let qualifs = self.tcx.mir_const_qualif(def_id);

record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Const(qualifs, const_data));
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
record!(self.tables.visibility[def_id.to_def_id()] <- ty::Visibility::Public);
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
self.encode_item_type(def_id.to_def_id());
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ define_tables! {

#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
enum EntryKind {
AnonConst(mir::ConstQualifs, Lazy<RenderedConst>),
Const(mir::ConstQualifs, Lazy<RenderedConst>),
ImmStatic,
MutStatic,
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/const-generics/auxiliary/impl-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(const_generics)]

pub struct Num<const N: usize>;

// Braces around const expression causes crash
impl Num<{5}> {
pub fn five(&self) {
}
}
14 changes: 14 additions & 0 deletions src/test/ui/const-generics/issue-68104-print-stack-overflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// aux-build:impl-const.rs
// run-pass

#![feature(const_generics)]
#![allow(incomplete_features)]

extern crate impl_const;

use impl_const::*;

pub fn main() {
let n = Num::<5>;
n.five();
}

0 comments on commit 52e8dcb

Please sign in to comment.