Update with comments
A bunch of nits fixed, and a new test for pretty printing the AST.
This commit is contained in:
parent
9fe793ae5d
commit
ea2af70466
11 changed files with 69 additions and 17 deletions
|
@ -2660,11 +2660,10 @@ impl<'a> State<'a> {
|
||||||
s.print_type(ty);
|
s.print_type(ty);
|
||||||
s.print_type_bounds(":", ¶m.bounds);
|
s.print_type_bounds(":", ¶m.bounds);
|
||||||
// FIXME(const_generic_defaults)
|
// FIXME(const_generic_defaults)
|
||||||
if let Some(ref _default) = default {
|
if let Some(ref default) = default {
|
||||||
// FIXME(const_generics_defaults): print the `default` value here
|
|
||||||
s.s.space();
|
s.s.space();
|
||||||
s.word_space("=");
|
s.word_space("=");
|
||||||
// s.print_anon_const(&default);
|
s.print_expr(&default.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -963,10 +963,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
.rev()
|
.rev()
|
||||||
.filter_map(|param| match param.kind {
|
.filter_map(|param| match param.kind {
|
||||||
ty::GenericParamDefKind::Lifetime => None,
|
ty::GenericParamDefKind::Lifetime => None,
|
||||||
ty::GenericParamDefKind::Const { has_default }
|
ty::GenericParamDefKind::Type { has_default, .. } => {
|
||||||
| ty::GenericParamDefKind::Type { has_default, .. } => {
|
|
||||||
Some((param.def_id, has_default))
|
Some((param.def_id, has_default))
|
||||||
}
|
}
|
||||||
|
// FIXME(const_generics:defaults)
|
||||||
|
ty::GenericParamDefKind::Const { has_default: _has_default } => None,
|
||||||
})
|
})
|
||||||
.peekable();
|
.peekable();
|
||||||
let has_default = {
|
let has_default = {
|
||||||
|
|
|
@ -122,7 +122,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
|
||||||
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
|
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
|
||||||
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }
|
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }
|
||||||
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
|
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
|
||||||
const_param_default => { tcx.arena.alloc(cdata.get_const_param_default(tcx, def_id.index)) }
|
const_param_default => { tcx.mk_const(cdata.get_const_param_default(tcx, def_id.index)) }
|
||||||
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
|
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
|
||||||
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
|
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
|
||||||
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
|
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
|
||||||
|
|
|
@ -307,6 +307,7 @@ define_tables! {
|
||||||
mir_for_ctfe: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
|
mir_for_ctfe: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
|
||||||
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
|
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
|
||||||
mir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [mir::abstract_const::Node<'tcx>])>,
|
mir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [mir::abstract_const::Node<'tcx>])>,
|
||||||
|
const_defaults: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
|
||||||
unused_generic_params: Table<DefIndex, Lazy<FiniteBitSet<u32>>>,
|
unused_generic_params: Table<DefIndex, Lazy<FiniteBitSet<u32>>>,
|
||||||
// `def_keys` and `def_path_hashes` represent a lazy version of a
|
// `def_keys` and `def_path_hashes` represent a lazy version of a
|
||||||
// `DefPathTable`. This allows us to avoid deserializing an entire
|
// `DefPathTable`. This allows us to avoid deserializing an entire
|
||||||
|
@ -314,7 +315,6 @@ define_tables! {
|
||||||
// definitions from any given crate.
|
// definitions from any given crate.
|
||||||
def_keys: Table<DefIndex, Lazy<DefKey>>,
|
def_keys: Table<DefIndex, Lazy<DefKey>>,
|
||||||
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>,
|
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>,
|
||||||
const_defaults: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
|
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
|
||||||
|
|
|
@ -206,8 +206,7 @@ impl<'tcx> Const<'tcx> {
|
||||||
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
|
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
|
||||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||||
let default_def_id = match tcx.hir().get(hir_id) {
|
let default_def_id = match tcx.hir().get(hir_id) {
|
||||||
hir::Node::AnonConst(ac)
|
hir::Node::GenericParam(hir::GenericParam {
|
||||||
| hir::Node::GenericParam(hir::GenericParam {
|
|
||||||
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
|
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
|
||||||
..
|
..
|
||||||
}) => tcx.hir().local_def_id(ac.hir_id),
|
}) => tcx.hir().local_def_id(ac.hir_id),
|
||||||
|
|
|
@ -120,10 +120,12 @@ impl<'tcx> Generics {
|
||||||
for param in &self.params {
|
for param in &self.params {
|
||||||
match param.kind {
|
match param.kind {
|
||||||
GenericParamDefKind::Lifetime => (),
|
GenericParamDefKind::Lifetime => (),
|
||||||
GenericParamDefKind::Type { has_default, .. }
|
GenericParamDefKind::Type { has_default, .. } => {
|
||||||
| GenericParamDefKind::Const { has_default } => {
|
|
||||||
own_defaults.types += has_default as usize;
|
own_defaults.types += has_default as usize;
|
||||||
}
|
}
|
||||||
|
GenericParamDefKind::Const { has_default } => {
|
||||||
|
own_defaults.consts += has_default as usize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -931,10 +931,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
|
||||||
GenericParamDefKind::Const { has_default, .. } => {
|
GenericParamDefKind::Const { has_default, .. } => {
|
||||||
self.visit(self.ev.tcx.type_of(param.def_id));
|
self.visit(self.ev.tcx.type_of(param.def_id));
|
||||||
if has_default {
|
if has_default {
|
||||||
// FIXME(const_generic_defaults)
|
self.visit(self.ev.tcx.const_param_default(param.def_id));
|
||||||
// how should the error case be handled here?
|
|
||||||
// let default_const = self.ev.tcx.const_eval_poly(param.def_id).unwrap();
|
|
||||||
// self.visit(default_const);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1747,6 +1744,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
|
||||||
self.visit(self.tcx.type_of(param.def_id));
|
self.visit(self.tcx.type_of(param.def_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// FIXME(const_evaluatable_checked): May want to look inside const here
|
||||||
GenericParamDefKind::Const { .. } => {
|
GenericParamDefKind::Const { .. } => {
|
||||||
self.visit(self.tcx.type_of(param.def_id));
|
self.visit(self.tcx.type_of(param.def_id));
|
||||||
}
|
}
|
||||||
|
|
|
@ -258,9 +258,9 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
|
||||||
let def_id = self.tcx.hir().local_def_id(param.hir_id);
|
let def_id = self.tcx.hir().local_def_id(param.hir_id);
|
||||||
self.tcx.ensure().type_of(def_id);
|
self.tcx.ensure().type_of(def_id);
|
||||||
if let Some(default) = default {
|
if let Some(default) = default {
|
||||||
let def_id = self.tcx.hir().local_def_id(default.hir_id);
|
let default_def_id = self.tcx.hir().local_def_id(default.hir_id);
|
||||||
// need to store default and type of default
|
// need to store default and type of default
|
||||||
self.tcx.ensure().type_of(def_id);
|
self.tcx.ensure().type_of(default_def_id);
|
||||||
self.tcx.ensure().const_param_default(def_id);
|
self.tcx.ensure().const_param_default(def_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/test/ui/const-generics/defaults/default-annotation.rs
Normal file
20
src/test/ui/const-generics/defaults/default-annotation.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
// run-pass
|
||||||
|
#![feature(staged_api)]
|
||||||
|
|
||||||
|
#![feature(const_generics)]
|
||||||
|
#![feature(const_generics_defaults)]
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
|
||||||
|
#![stable(feature = "const_default_test", since="none")]
|
||||||
|
|
||||||
|
|
||||||
|
#[unstable(feature = "const_default_stable", issue="none")]
|
||||||
|
pub struct ConstDefaultUnstable<const N: usize = 3>;
|
||||||
|
|
||||||
|
#[stable(feature = "const_default_unstable", since="none")]
|
||||||
|
pub struct ConstDefaultStable<const N: usize = {
|
||||||
|
#[stable(feature = "const_default_unstable_val", since="none")]
|
||||||
|
3
|
||||||
|
}>;
|
||||||
|
|
||||||
|
fn main() {}
|
13
src/test/ui/const-generics/defaults/pretty-printing-ast.rs
Normal file
13
src/test/ui/const-generics/defaults/pretty-printing-ast.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// Test the AST pretty printer correctly handles default values for const generics
|
||||||
|
// check-pass
|
||||||
|
// compile-flags: -Z unpretty=expanded
|
||||||
|
|
||||||
|
#![crate_type = "lib"]
|
||||||
|
#![feature(const_generics_defaults)]
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
|
||||||
|
trait Foo<const KIND: bool = true> {}
|
||||||
|
|
||||||
|
fn foo<const SIZE: usize = 5>() {}
|
||||||
|
|
||||||
|
struct Range<const FROM: usize = 0, const LEN: usize = 0, const TO: usize = {FROM + LEN}>;
|
|
@ -0,0 +1,20 @@
|
||||||
|
#![feature(prelude_import)]
|
||||||
|
#![no_std]
|
||||||
|
// Test the AST pretty printer correctly handles default values for const generics
|
||||||
|
// check-pass
|
||||||
|
// compile-flags: -Z unpretty=expanded
|
||||||
|
|
||||||
|
#![crate_type = "lib"]
|
||||||
|
#![feature(const_generics_defaults)]
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
#[prelude_import]
|
||||||
|
use ::std::prelude::rust_2015::*;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate std;
|
||||||
|
|
||||||
|
trait Foo<const KIND : bool = true> { }
|
||||||
|
|
||||||
|
fn foo<const SIZE : usize = 5>() { }
|
||||||
|
|
||||||
|
struct Range<const FROM : usize = 0, const LEN : usize = 0, const TO : usize =
|
||||||
|
{ FROM + LEN }>;
|
Loading…
Add table
Reference in a new issue