Skip to content

Commit 8d1123d

Browse files
authored
Rollup merge of rust-lang#65365 - skinny121:const-args-metadata, r=varkor
Include const generic arguments in metadata Fixes rust-lang#64707 Fixes rust-lang#61624 Fixes rust-lang#64730 r? @varkor
2 parents 6241a4a + eb68bbb commit 8d1123d

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

src/librustc_metadata/encoder.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
55
EncodedMetadata, ForeignModule};
66
use rustc::hir::def::CtorKind;
77
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
8-
use rustc::hir::GenericParamKind;
8+
use rustc::hir::{GenericParamKind, AnonConst};
99
use rustc::hir::map::definitions::DefPathTable;
1010
use rustc_data_structures::fingerprint::Fingerprint;
1111
use rustc_index::vec::IndexVec;
@@ -1712,6 +1712,11 @@ impl Visitor<'tcx> for EncodeContext<'tcx> {
17121712
intravisit::walk_expr(self, ex);
17131713
self.encode_info_for_expr(ex);
17141714
}
1715+
fn visit_anon_const(&mut self, c: &'tcx AnonConst) {
1716+
intravisit::walk_anon_const(self, c);
1717+
let def_id = self.tcx.hir().local_def_id(c.hir_id);
1718+
self.record(def_id, EncodeContext::encode_info_for_anon_const, def_id);
1719+
}
17151720
fn visit_item(&mut self, item: &'tcx hir::Item) {
17161721
intravisit::walk_item(self, item);
17171722
let def_id = self.tcx.hir().local_def_id(item.hir_id);
@@ -1729,25 +1734,10 @@ impl Visitor<'tcx> for EncodeContext<'tcx> {
17291734
EncodeContext::encode_info_for_foreign_item,
17301735
(def_id, ni));
17311736
}
1732-
fn visit_variant(&mut self,
1733-
v: &'tcx hir::Variant,
1734-
g: &'tcx hir::Generics,
1735-
id: hir::HirId) {
1736-
intravisit::walk_variant(self, v, g, id);
1737-
1738-
if let Some(ref discr) = v.disr_expr {
1739-
let def_id = self.tcx.hir().local_def_id(discr.hir_id);
1740-
self.record(def_id, EncodeContext::encode_info_for_anon_const, def_id);
1741-
}
1742-
}
17431737
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
17441738
intravisit::walk_generics(self, generics);
17451739
self.encode_info_for_generics(generics);
17461740
}
1747-
fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
1748-
intravisit::walk_ty(self, ty);
1749-
self.encode_info_for_ty(ty);
1750-
}
17511741
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef) {
17521742
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id);
17531743
self.record(def_id, EncodeContext::encode_info_for_macro_def, macro_def);
@@ -1785,16 +1775,6 @@ impl EncodeContext<'tcx> {
17851775
}
17861776
}
17871777

1788-
fn encode_info_for_ty(&mut self, ty: &hir::Ty) {
1789-
match ty.kind {
1790-
hir::TyKind::Array(_, ref length) => {
1791-
let def_id = self.tcx.hir().local_def_id(length.hir_id);
1792-
self.record(def_id, EncodeContext::encode_info_for_anon_const, def_id);
1793-
}
1794-
_ => {}
1795-
}
1796-
}
1797-
17981778
fn encode_info_for_expr(&mut self, expr: &hir::Expr) {
17991779
match expr.kind {
18001780
hir::ExprKind::Closure(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(const_generics)]
2+
3+
pub struct Struct<const N: usize>(pub [u8; N]);
4+
5+
pub type Alias = Struct<2>;
6+
7+
pub fn function(value: Struct<3>) -> u8 {
8+
value.0[0]
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// aux-build:const_generic_lib.rs
2+
3+
extern crate const_generic_lib;
4+
5+
fn main() {
6+
let _ = const_generic_lib::function(const_generic_lib::Struct([0u8, 1u8]));
7+
//~^ ERROR mismatched types
8+
let _: const_generic_lib::Alias = const_generic_lib::Struct([0u8, 1u8, 2u8]);
9+
//~^ ERROR mismatched types
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/const-argument-cross-crate-mismatch.rs:6:41
3+
|
4+
LL | let _ = const_generic_lib::function(const_generic_lib::Struct([0u8, 1u8]));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `2usize`
6+
|
7+
= note: expected type `const_generic_lib::Struct<3usize>`
8+
found type `const_generic_lib::Struct<_: usize>`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/const-argument-cross-crate-mismatch.rs:8:39
12+
|
13+
LL | let _: const_generic_lib::Alias = const_generic_lib::Struct([0u8, 1u8, 2u8]);
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2usize`, found `3usize`
15+
|
16+
= note: expected type `const_generic_lib::Struct<2usize>`
17+
found type `const_generic_lib::Struct<_: usize>`
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-pass
2+
// aux-build:const_generic_lib.rs
3+
4+
extern crate const_generic_lib;
5+
6+
struct Container(const_generic_lib::Alias);
7+
8+
fn main() {
9+
let res = const_generic_lib::function(const_generic_lib::Struct([14u8, 1u8, 2u8]));
10+
assert_eq!(res, 14u8);
11+
let _ = Container(const_generic_lib::Struct([0u8, 1u8]));
12+
}

0 commit comments

Comments
 (0)