Skip to content

Commit ea2af70

Browse files
committed
Update with comments
A bunch of nits fixed, and a new test for pretty printing the AST.
1 parent 9fe793a commit ea2af70

File tree

11 files changed

+69
-17
lines changed

11 files changed

+69
-17
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,11 +2660,10 @@ impl<'a> State<'a> {
26602660
s.print_type(ty);
26612661
s.print_type_bounds(":", &param.bounds);
26622662
// FIXME(const_generic_defaults)
2663-
if let Some(ref _default) = default {
2664-
// FIXME(const_generics_defaults): print the `default` value here
2663+
if let Some(ref default) = default {
26652664
s.s.space();
26662665
s.word_space("=");
2667-
// s.print_anon_const(&default);
2666+
s.print_expr(&default.value);
26682667
}
26692668
}
26702669
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
963963
.rev()
964964
.filter_map(|param| match param.kind {
965965
ty::GenericParamDefKind::Lifetime => None,
966-
ty::GenericParamDefKind::Const { has_default }
967-
| ty::GenericParamDefKind::Type { has_default, .. } => {
966+
ty::GenericParamDefKind::Type { has_default, .. } => {
968967
Some((param.def_id, has_default))
969968
}
969+
// FIXME(const_generics:defaults)
970+
ty::GenericParamDefKind::Const { has_default: _has_default } => None,
970971
})
971972
.peekable();
972973
let has_default = {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
122122
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
123123
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }
124124
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
125-
const_param_default => { tcx.arena.alloc(cdata.get_const_param_default(tcx, def_id.index)) }
125+
const_param_default => { tcx.mk_const(cdata.get_const_param_default(tcx, def_id.index)) }
126126
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
127127
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
128128
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,14 @@ define_tables! {
307307
mir_for_ctfe: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
308308
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
309309
mir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [mir::abstract_const::Node<'tcx>])>,
310+
const_defaults: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
310311
unused_generic_params: Table<DefIndex, Lazy<FiniteBitSet<u32>>>,
311312
// `def_keys` and `def_path_hashes` represent a lazy version of a
312313
// `DefPathTable`. This allows us to avoid deserializing an entire
313314
// `DefPathTable` up front, since we may only ever use a few
314315
// definitions from any given crate.
315316
def_keys: Table<DefIndex, Lazy<DefKey>>,
316317
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>,
317-
const_defaults: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
318318
}
319319

320320
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ impl<'tcx> Const<'tcx> {
206206
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
207207
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
208208
let default_def_id = match tcx.hir().get(hir_id) {
209-
hir::Node::AnonConst(ac)
210-
| hir::Node::GenericParam(hir::GenericParam {
209+
hir::Node::GenericParam(hir::GenericParam {
211210
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
212211
..
213212
}) => tcx.hir().local_def_id(ac.hir_id),

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ impl<'tcx> Generics {
120120
for param in &self.params {
121121
match param.kind {
122122
GenericParamDefKind::Lifetime => (),
123-
GenericParamDefKind::Type { has_default, .. }
124-
| GenericParamDefKind::Const { has_default } => {
123+
GenericParamDefKind::Type { has_default, .. } => {
125124
own_defaults.types += has_default as usize;
126125
}
126+
GenericParamDefKind::Const { has_default } => {
127+
own_defaults.consts += has_default as usize;
128+
}
127129
}
128130
}
129131

compiler/rustc_privacy/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -931,10 +931,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
931931
GenericParamDefKind::Const { has_default, .. } => {
932932
self.visit(self.ev.tcx.type_of(param.def_id));
933933
if has_default {
934-
// FIXME(const_generic_defaults)
935-
// how should the error case be handled here?
936-
// let default_const = self.ev.tcx.const_eval_poly(param.def_id).unwrap();
937-
// self.visit(default_const);
934+
self.visit(self.ev.tcx.const_param_default(param.def_id));
938935
}
939936
}
940937
}
@@ -1747,6 +1744,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
17471744
self.visit(self.tcx.type_of(param.def_id));
17481745
}
17491746
}
1747+
// FIXME(const_evaluatable_checked): May want to look inside const here
17501748
GenericParamDefKind::Const { .. } => {
17511749
self.visit(self.tcx.type_of(param.def_id));
17521750
}

compiler/rustc_typeck/src/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
258258
let def_id = self.tcx.hir().local_def_id(param.hir_id);
259259
self.tcx.ensure().type_of(def_id);
260260
if let Some(default) = default {
261-
let def_id = self.tcx.hir().local_def_id(default.hir_id);
261+
let default_def_id = self.tcx.hir().local_def_id(default.hir_id);
262262
// need to store default and type of default
263-
self.tcx.ensure().type_of(def_id);
263+
self.tcx.ensure().type_of(default_def_id);
264264
self.tcx.ensure().const_param_default(def_id);
265265
}
266266
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-pass
2+
#![feature(staged_api)]
3+
4+
#![feature(const_generics)]
5+
#![feature(const_generics_defaults)]
6+
#![allow(incomplete_features)]
7+
8+
#![stable(feature = "const_default_test", since="none")]
9+
10+
11+
#[unstable(feature = "const_default_stable", issue="none")]
12+
pub struct ConstDefaultUnstable<const N: usize = 3>;
13+
14+
#[stable(feature = "const_default_unstable", since="none")]
15+
pub struct ConstDefaultStable<const N: usize = {
16+
#[stable(feature = "const_default_unstable_val", since="none")]
17+
3
18+
}>;
19+
20+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Test the AST pretty printer correctly handles default values for const generics
2+
// check-pass
3+
// compile-flags: -Z unpretty=expanded
4+
5+
#![crate_type = "lib"]
6+
#![feature(const_generics_defaults)]
7+
#![allow(incomplete_features)]
8+
9+
trait Foo<const KIND: bool = true> {}
10+
11+
fn foo<const SIZE: usize = 5>() {}
12+
13+
struct Range<const FROM: usize = 0, const LEN: usize = 0, const TO: usize = {FROM + LEN}>;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
// Test the AST pretty printer correctly handles default values for const generics
4+
// check-pass
5+
// compile-flags: -Z unpretty=expanded
6+
7+
#![crate_type = "lib"]
8+
#![feature(const_generics_defaults)]
9+
#![allow(incomplete_features)]
10+
#[prelude_import]
11+
use ::std::prelude::rust_2015::*;
12+
#[macro_use]
13+
extern crate std;
14+
15+
trait Foo<const KIND : bool = true> { }
16+
17+
fn foo<const SIZE : usize = 5>() { }
18+
19+
struct Range<const FROM : usize = 0, const LEN : usize = 0, const TO : usize =
20+
{ FROM + LEN }>;

0 commit comments

Comments
 (0)