Skip to content

Commit

Permalink
Include const generic arguments in metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinnyBat committed Oct 14, 2019
1 parent 446e5e5 commit eb68bbb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
32 changes: 6 additions & 26 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
EncodedMetadata, ForeignModule};
use rustc::hir::def::CtorKind;
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
use rustc::hir::GenericParamKind;
use rustc::hir::{GenericParamKind, AnonConst};
use rustc::hir::map::definitions::DefPathTable;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_index::vec::IndexVec;
Expand Down Expand Up @@ -1711,6 +1711,11 @@ impl Visitor<'tcx> for EncodeContext<'tcx> {
intravisit::walk_expr(self, ex);
self.encode_info_for_expr(ex);
}
fn visit_anon_const(&mut self, c: &'tcx AnonConst) {
intravisit::walk_anon_const(self, c);
let def_id = self.tcx.hir().local_def_id(c.hir_id);
self.record(def_id, EncodeContext::encode_info_for_anon_const, def_id);
}
fn visit_item(&mut self, item: &'tcx hir::Item) {
intravisit::walk_item(self, item);
let def_id = self.tcx.hir().local_def_id(item.hir_id);
Expand All @@ -1728,25 +1733,10 @@ impl Visitor<'tcx> for EncodeContext<'tcx> {
EncodeContext::encode_info_for_foreign_item,
(def_id, ni));
}
fn visit_variant(&mut self,
v: &'tcx hir::Variant,
g: &'tcx hir::Generics,
id: hir::HirId) {
intravisit::walk_variant(self, v, g, id);

if let Some(ref discr) = v.disr_expr {
let def_id = self.tcx.hir().local_def_id(discr.hir_id);
self.record(def_id, EncodeContext::encode_info_for_anon_const, def_id);
}
}
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
intravisit::walk_generics(self, generics);
self.encode_info_for_generics(generics);
}
fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
intravisit::walk_ty(self, ty);
self.encode_info_for_ty(ty);
}
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef) {
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id);
self.record(def_id, EncodeContext::encode_info_for_macro_def, macro_def);
Expand Down Expand Up @@ -1784,16 +1774,6 @@ impl EncodeContext<'tcx> {
}
}

fn encode_info_for_ty(&mut self, ty: &hir::Ty) {
match ty.kind {
hir::TyKind::Array(_, ref length) => {
let def_id = self.tcx.hir().local_def_id(length.hir_id);
self.record(def_id, EncodeContext::encode_info_for_anon_const, def_id);
}
_ => {}
}
}

fn encode_info_for_expr(&mut self, expr: &hir::Expr) {
match expr.kind {
hir::ExprKind::Closure(..) => {
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/const-generics/auxiliary/const_generic_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(const_generics)]

pub struct Struct<const N: usize>(pub [u8; N]);

pub type Alias = Struct<2>;

pub fn function(value: Struct<3>) -> u8 {
value.0[0]
}
10 changes: 10 additions & 0 deletions src/test/ui/const-generics/const-argument-cross-crate-mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// aux-build:const_generic_lib.rs

extern crate const_generic_lib;

fn main() {
let _ = const_generic_lib::function(const_generic_lib::Struct([0u8, 1u8]));
//~^ ERROR mismatched types
let _: const_generic_lib::Alias = const_generic_lib::Struct([0u8, 1u8, 2u8]);
//~^ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0308]: mismatched types
--> $DIR/const-argument-cross-crate-mismatch.rs:6:41
|
LL | let _ = const_generic_lib::function(const_generic_lib::Struct([0u8, 1u8]));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `2usize`
|
= note: expected type `const_generic_lib::Struct<3usize>`
found type `const_generic_lib::Struct<_: usize>`

error[E0308]: mismatched types
--> $DIR/const-argument-cross-crate-mismatch.rs:8:39
|
LL | let _: const_generic_lib::Alias = const_generic_lib::Struct([0u8, 1u8, 2u8]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2usize`, found `3usize`
|
= note: expected type `const_generic_lib::Struct<2usize>`
found type `const_generic_lib::Struct<_: usize>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
12 changes: 12 additions & 0 deletions src/test/ui/const-generics/const-argument-cross-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// run-pass
// aux-build:const_generic_lib.rs

extern crate const_generic_lib;

struct Container(const_generic_lib::Alias);

fn main() {
let res = const_generic_lib::function(const_generic_lib::Struct([14u8, 1u8, 2u8]));
assert_eq!(res, 14u8);
let _ = Container(const_generic_lib::Struct([0u8, 1u8]));
}

0 comments on commit eb68bbb

Please sign in to comment.