Skip to content

Commit 7116bb5

Browse files
committed
Update with comments
1 parent ea2af70 commit 7116bb5

File tree

9 files changed

+72
-33
lines changed

9 files changed

+72
-33
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,6 @@ impl<'a> State<'a> {
26592659
s.word_space(":");
26602660
s.print_type(ty);
26612661
s.print_type_bounds(":", &param.bounds);
2662-
// FIXME(const_generic_defaults)
26632662
if let Some(ref default) = default {
26642663
s.s.space();
26652664
s.word_space("=");

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+36-25
Original file line numberDiff line numberDiff line change
@@ -957,34 +957,45 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
957957
) -> SubstsRef<'tcx> {
958958
let generics = self.tcx.generics_of(def_id);
959959
let mut num_supplied_defaults = 0;
960-
let mut type_params = generics
961-
.params
962-
.iter()
963-
.rev()
964-
.filter_map(|param| match param.kind {
965-
ty::GenericParamDefKind::Lifetime => None,
966-
ty::GenericParamDefKind::Type { has_default, .. } => {
967-
Some((param.def_id, has_default))
968-
}
969-
// FIXME(const_generics:defaults)
970-
ty::GenericParamDefKind::Const { has_default: _has_default } => None,
971-
})
972-
.peekable();
973-
let has_default = {
974-
let has_default = type_params.peek().map(|(_, has_default)| has_default);
975-
*has_default.unwrap_or(&false)
976-
};
977-
if has_default {
978-
let types = substs.types().rev();
979-
for ((def_id, has_default), actual) in type_params.zip(types) {
980-
if !has_default {
981-
break;
960+
961+
#[derive(PartialEq, Eq, Copy, Clone)]
962+
enum Kind {
963+
Const,
964+
Type,
965+
}
966+
let default_params = generics.params.iter().rev().filter_map(|param| match param.kind {
967+
ty::GenericParamDefKind::Type { has_default: true, .. } => {
968+
Some((param.def_id, Kind::Type))
969+
}
970+
ty::GenericParamDefKind::Const { has_default: true } => {
971+
Some((param.def_id, Kind::Const))
972+
}
973+
_ => None,
974+
});
975+
let mut types = substs.types().rev();
976+
let mut consts = substs.consts().rev();
977+
for (def_id, kind) in default_params {
978+
match kind {
979+
Kind::Const => {
980+
if let Some(actual) = consts.next() {
981+
if ty::Const::from_anon_const(self.tcx, def_id.expect_local()) != actual {
982+
break;
983+
}
984+
} else {
985+
break;
986+
}
982987
}
983-
if self.tcx.type_of(def_id).subst(self.tcx, substs) != actual {
984-
break;
988+
Kind::Type => {
989+
if let Some(actual) = types.next() {
990+
if self.tcx.type_of(def_id).subst(self.tcx, substs) != actual {
991+
break;
992+
}
993+
} else {
994+
break;
995+
}
985996
}
986-
num_supplied_defaults += 1;
987997
}
998+
num_supplied_defaults += 1;
988999
}
9891000
let len = generics.params.len();
9901001
let mut generics = generics.clone();

compiler/rustc_metadata/src/rmeta/encoder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,6 @@ impl EncodeContext<'a, 'tcx> {
18801880
let def_id = def_id.to_def_id();
18811881
self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true);
18821882
if default.is_some() {
1883-
self.encode_stability(def_id);
18841883
record!(self.tables.const_defaults[def_id] <- self.tcx.const_param_default(def_id))
18851884
}
18861885
}

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ rustc_queries! {
9494
}
9595

9696
/// Given the def_id of a const-generic parameter, computes the associated default const
97-
/// parameter. i.e. `fn example<const N: usize=3>` called on N would return 3.
97+
/// parameter. e.g. `fn example<const N: usize=3>` called on `N` would return `3`.
9898
query const_param_default(param: DefId) -> &'tcx ty::Const<'tcx> {
9999
desc { |tcx| "compute const default for a given parameter `{}`", tcx.def_path_str(param) }
100100
}

compiler/rustc_middle/src/ty/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Cons
212212
}) => tcx.hir().local_def_id(ac.hir_id),
213213
_ => span_bug!(
214214
tcx.def_span(def_id),
215-
"const_param_defaults expected a generic parameter with a constant"
215+
"`const_param_default` expected a generic parameter with a constant"
216216
),
217217
};
218218
Const::from_anon_const(tcx, default_def_id)

compiler/rustc_typeck/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ fn check_where_clauses<'tcx, 'fcx>(
775775
GenericParamDefKind::Const { .. } => {
776776
if is_our_default(param) {
777777
let default_ct = tcx.const_param_default(param.def_id);
778-
// Const params have to currently be concrete.
778+
// Const params currently have to be concrete.
779779
assert!(!default_ct.needs_subst());
780780
default_ct.into()
781781
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "lib"]
2+
#![feature(const_generics)]
3+
#![feature(const_generics_defaults)]
4+
#![allow(incomplete_features, dead_code)]
5+
6+
struct Both<const N: usize=3, T> {
7+
//~^ ERROR: generic parameters with a default must be
8+
v: T
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: generic parameters with a default must be trailing
2+
--> $DIR/const_default_first.rs:6:19
3+
|
4+
LL | struct Both<const N: usize=3, T> {
5+
| ^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// check-pass
2-
#![crate_type = "lib"]
1+
// run-pass
2+
#![feature(const_generics)]
33
#![feature(const_generics_defaults)]
4-
#![allow(incomplete_features)]
4+
#![allow(incomplete_features, dead_code)]
55

66
struct Both<T=u32, const N: usize=3> {
77
arr: [T; N]
@@ -12,3 +12,16 @@ trait BothTrait<T=u32, const N: usize=3> {}
1212
enum BothEnum<T=u32, const N: usize=3> {
1313
Dummy([T; N])
1414
}
15+
16+
struct OppOrder<const N: usize=3, T=u32> {
17+
arr: [T; N]
18+
}
19+
20+
fn main() {
21+
let _ = OppOrder::<3, u32> {
22+
arr: [0,0,0],
23+
};
24+
let _ = Both::<u8, 1> {
25+
arr: [0],
26+
};
27+
}

0 commit comments

Comments
 (0)