提交 8ef81388 编写于 作者: V varkor 提交者: kadmin

Some refactoring

上级 e4e5db4e
......@@ -785,8 +785,8 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
visit_opt(default, |default| vis.visit_ty(default));
}
GenericParamKind::Const { ty, kw_span: _, default } => {
visit_opt(default, |default| vis.visit_anon_const(default));
vis.visit_ty(ty);
visit_opt(default, |default| vis.visit_anon_const(default));
}
}
smallvec![param]
......
......@@ -1180,7 +1180,7 @@ fn visit_generics(&mut self, generics: &'a Generics) {
default.value.span,
"default values for const generic parameters are unstable",
);
err.note("to enable them use #![feature(const_generic_defaults)]");
err.help("add `#![feature(const_generic_defaults)]` to the crate attributes to enable");
err.emit();
break;
}
......
......@@ -2659,6 +2659,7 @@ pub fn print_type_bounds(&mut self, prefix: &'static str, bounds: &[ast::Generic
s.word_space(":");
s.print_type(ty);
s.print_type_bounds(":", &param.bounds);
// FIXME(const_generic_defaults)
if let Some(ref _default) = default {
// FIXME(const_generics_defaults): print the `default` value here
s.s.space();
......
......@@ -963,9 +963,8 @@ fn strip_generic_default_params(
.rev()
.filter_map(|param| match param.kind {
ty::GenericParamDefKind::Lifetime => None,
ty::GenericParamDefKind::Type { has_default, .. }
| ty::GenericParamDefKind::Const { has_default } => {
ty::GenericParamDefKind::Const { has_default }
| ty::GenericParamDefKind::Type { has_default, .. } => {
Some((param.def_id, has_default))
}
})
......
......@@ -1876,13 +1876,16 @@ fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) {
default.is_some(),
);
}
GenericParamKind::Const { .. } => {
GenericParamKind::Const { ref default, .. } => {
self.encode_info_for_generic_param(
def_id.to_def_id(),
EntryKind::ConstParam,
true,
);
// FIXME(const_generics_defaults)
if default.is_some() {
self.encode_stability(def_id.to_def_id());
}
// FIXME(const_generic_defaults)
}
}
}
......
......@@ -507,10 +507,9 @@ fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
let kind = match &p.kind {
// FIXME(const_generics_defaults)
hir::GenericParamKind::Type { default, .. } if default.is_some() => {
AnnotationKind::Container
}
// Allow stability attributes on default generic arguments.
hir::GenericParamKind::Type { default: Some(_), .. }
| hir::GenericParamKind::Const { default: Some(_), .. } => AnnotationKind::Container,
_ => AnnotationKind::Prohibited,
};
......
......@@ -931,6 +931,7 @@ fn generics(&mut self) -> &mut Self {
GenericParamDefKind::Const { has_default, .. } => {
self.visit(self.ev.tcx.type_of(param.def_id));
if has_default {
// FIXME(const_generic_defaults)
// 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);
......
......@@ -450,12 +450,12 @@ impl<'a> Resolver<'a> {
self.session,
span,
E0128,
"type parameters with a default cannot use \
"generic parameters with a default cannot use \
forward declared identifiers"
);
err.span_label(
span,
"defaulted type parameters cannot be forward declared".to_string(),
"defaulted generic parameters cannot be forward declared".to_string(),
);
err
}
......
......@@ -132,10 +132,10 @@ enum PatBoundCtx {
/// We passed through a `macro_rules!` statement
MacroDefinition(DefId),
/// All bindings in this rib are type parameters that can't be used
/// from the default of a type parameter because they're not declared
/// before said type parameter. Also see the `visit_generics` override.
ForwardTyParamBanRibKind,
/// All bindings in this rib are generic parameters that can't be used
/// from the default of a generic parameter because they're not declared
/// before said generic parameter. Also see the `visit_generics` override.
ForwardGenericParamBanRibKind,
/// We are inside of the type of a const parameter. Can't refer to any
/// parameters.
......@@ -154,7 +154,7 @@ impl RibKind<'_> {
| ModuleRibKind(_)
| MacroDefinition(_)
| ConstParamTyRibKind => false,
AssocItemRibKind | ItemRibKind(_) | ForwardTyParamBanRibKind => true,
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
}
}
}
......@@ -555,15 +555,16 @@ fn visit_generics(&mut self, generics: &'ast Generics) {
// provide previous type parameters as they're built. We
// put all the parameters on the ban list and then remove
// them one by one as they are processed and become available.
let mut default_ban_rib = Rib::new(ForwardTyParamBanRibKind);
let mut default_ban_rib = Rib::new(ForwardGenericParamBanRibKind);
let mut found_default = false;
default_ban_rib.bindings.extend(generics.params.iter().filter_map(
|param| match param.kind {
GenericParamKind::Const { .. } | GenericParamKind::Lifetime { .. } => None,
GenericParamKind::Type { ref default, .. } => {
found_default |= default.is_some();
found_default.then_some((Ident::with_dummy_span(param.ident.name), Res::Err))
GenericParamKind::Type { default: Some(_), .. }
| GenericParamKind::Const { default: Some(_), .. } => {
found_default = true;
Some((Ident::with_dummy_span(param.ident.name), Res::Err))
}
_ => None,
},
));
......@@ -591,8 +592,8 @@ fn visit_generics(&mut self, generics: &'ast Generics) {
if let Some(ref ty) = default {
self.ribs[TypeNS].push(default_ban_rib);
self.with_rib(ValueNS, ForwardTyParamBanRibKind, |this| {
// HACK: We use an empty `ForwardTyParamBanRibKind` here which
self.with_rib(ValueNS, ForwardGenericParamBanRibKind, |this| {
// HACK: We use an empty `ForwardGenericParamBanRibKind` here which
// is only used to forbid the use of const parameters inside of
// type defaults.
//
......@@ -616,7 +617,6 @@ fn visit_generics(&mut self, generics: &'ast Generics) {
self.visit_ty(ty);
self.ribs[TypeNS].pop().unwrap();
self.ribs[ValueNS].pop().unwrap();
// FIXME(const_generics:default) do something with default here?
}
}
}
......@@ -866,7 +866,7 @@ fn is_label_valid_from_rib(&self, rib_index: usize) -> bool {
| ItemRibKind(..)
| ConstantItemRibKind(..)
| ModuleRibKind(..)
| ForwardTyParamBanRibKind
| ForwardGenericParamBanRibKind
| ConstParamTyRibKind => {
return false;
}
......
......@@ -2592,8 +2592,8 @@ fn validate_res_from_ribs(
debug!("validate_res_from_ribs({:?})", res);
let ribs = &all_ribs[rib_index + 1..];
// An invalid forward use of a type parameter from a previous default.
if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind {
// An invalid forward use of a generic parameter from a previous default.
if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
if record_used {
let res_error = if rib_ident.name == kw::SelfUpper {
ResolutionError::SelfInTyParamDefault
......@@ -2617,7 +2617,7 @@ fn validate_res_from_ribs(
| ClosureOrAsyncRibKind
| ModuleRibKind(..)
| MacroDefinition(..)
| ForwardTyParamBanRibKind => {
| ForwardGenericParamBanRibKind => {
// Nothing to do. Continue.
}
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
......@@ -2689,7 +2689,9 @@ fn validate_res_from_ribs(
// We only forbid constant items if we are inside of type defaults,
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
ForwardTyParamBanRibKind => {
ForwardGenericParamBanRibKind => {
// FIXME(const_generic_defaults): we may need to distinguish between
// being in type parameter defaults and const parameter defaults
in_ty_param_default = true;
continue;
}
......@@ -2782,7 +2784,9 @@ fn validate_res_from_ribs(
// We only forbid constant items if we are inside of type defaults,
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
ForwardTyParamBanRibKind => {
ForwardGenericParamBanRibKind => {
// FIXME(const_generic_defaults): we may need to distinguish between
// being in type parameter defaults and const parameter defaults
in_ty_param_default = true;
continue;
}
......
......@@ -507,7 +507,7 @@ fn inferred_kind(
GenericParamDefKind::Const { has_default } => {
let ty = tcx.at(self.span).type_of(param.def_id);
if !infer_args && has_default {
let c = ty::Const::from_anon_const(tcx, param.def_id.expect_local());
let c = substs.unwrap()[param.index as usize].expect_const();
ty::subst::GenericArg::from(c)
} else if infer_args {
self.astconv.ct_infer(ty, Some(param), self.span).into()
......@@ -515,6 +515,7 @@ fn inferred_kind(
// We've already errored above about the mismatch.
tcx.const_error(ty).into()
}
// FIXME(const_generic_defaults)
/*
if !infer_args && has_default {
/*
......
......@@ -1447,7 +1447,7 @@ fn inferred_kind(
if infer_args || !has_default {
return self.fcx.var_for_def(self.span, param);
}
// FIXME(const_generics:defaults)
// FIXME(const_generic_defaults)
// No const parameters were provided, we have to infer them.
todo!()
}
......
......@@ -713,10 +713,11 @@ fn check_where_clauses<'tcx, 'fcx>(
let generics = tcx.generics_of(def_id);
let is_our_default = |def: &ty::GenericParamDef| match def.kind {
GenericParamDefKind::Type { has_default, .. } => {
GenericParamDefKind::Type { has_default, .. }
| GenericParamDefKind::Const { has_default } => {
has_default && def.index >= generics.parent_count as u32
}
_ => unreachable!(),
GenericParamDefKind::Lifetime => unreachable!(),
};
// Check that concrete defaults are well-formed. See test `type-check-defaults.rs`.
......@@ -758,7 +759,7 @@ fn check_where_clauses<'tcx, 'fcx>(
fcx.tcx.mk_param_from_def(param)
}
GenericParamDefKind::Const { .. } | GenericParamDefKind::Type { .. } => {
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
// If the param has a default, ...
if is_our_default(param) {
let default_ty = fcx.tcx.type_of(param.def_id);
......
......@@ -254,10 +254,14 @@ fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
self.tcx.ensure().type_of(def_id);
}
hir::GenericParamKind::Type { .. } => {}
hir::GenericParamKind::Const { .. } => {
hir::GenericParamKind::Const { default, .. } => {
let def_id = self.tcx.hir().local_def_id(param.hir_id);
self.tcx.ensure().type_of(def_id);
// FIXME(const_generics_defaults)
if let Some(default) = default {
let def_id = self.tcx.hir().local_def_id(default.hir_id);
self.tcx.ensure().type_of(def_id);
}
}
}
}
......
......@@ -83,8 +83,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
return generics
.params
.iter()
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { ..
}))
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { .. }))
.nth(arg_index)
.map(|param| param.def_id);
}
......
......@@ -5,10 +5,7 @@
#![allow(incomplete_features)]
#[derive(Default)]
pub struct ConstDefault<const N: usize = 3> {
items: [u32; N]
}
pub struct ConstDefault<const N: usize = 3> {}
pub fn main() {
let s = ConstDefault::default();
......
#![feature(const_generic_defaults)]
#![crate_type = "lib"]
#![feature(const_generics_defaults)]
#![feature(min_const_generics)]
#![allow(incomplete_features)]
fn foo<const SIZE: usize = 5>() {}
//~^ ERROR default values for const generic parameters are experimental
fn main() {}
fn foo<const SIZE: usize = 5usize>() {}
error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_function_param.rs:1:26
error[E0282]: type annotations needed
--> $DIR/default_function_param.rs:6:28
|
LL | fn foo<const SIZE: usize = 5>() {}
| ^^^
|
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
| ^ cannot infer type for type `{integer}`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0282`.
trait Foo<const KIND: bool = true> {}
//~^ ERROR default values for const generic parameters are experimental
fn main() {}
error[E0128]: type parameters with a default cannot use forward declared identifiers
error[E0128]: generic parameters with a default cannot use forward declared identifiers
--> $DIR/E0128.rs:1:14
|
LL | struct Foo<T=U, U=()> {
| ^ defaulted type parameters cannot be forward declared
| ^ defaulted generic parameters cannot be forward declared
error: aborting due to previous error
......
#![feature(min_const_generics)]
#![crate_type="lib"]
struct A<const N: usize = 3>;
//~^ ERROR default values for
error: default values for const generic parameters are unstable
--> $DIR/feature-gate-const_generic_defaults.rs:4:27
|
LL | struct A<const N: usize = 3>;
| ^
|
= note: to enable them use #![feature(const_generic_defaults)]
error: aborting due to previous error
......@@ -10,12 +10,5 @@ error: type parameters with a default must be trailing
LL | struct Foo<A, B = Vec<C>, C>(A, B, C);
| ^
error[E0128]: type parameters with a default cannot use forward declared identifiers
--> $DIR/generic-non-trailing-defaults.rs:6:23
|
LL | struct Foo<A, B = Vec<C>, C>(A, B, C);
| ^ defaulted type parameters cannot be forward declared
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0128`.
error[E0128]: type parameters with a default cannot use forward declared identifiers
error[E0128]: generic parameters with a default cannot use forward declared identifiers
--> $DIR/generic-type-params-forward-mention.rs:2:23
|
LL | struct Foo<T = Option<U>, U = bool>(T, U);
| ^ defaulted type parameters cannot be forward declared
| ^ defaulted generic parameters cannot be forward declared
error: aborting due to previous error
......
error[E0128]: type parameters with a default cannot use forward declared identifiers
error[E0128]: generic parameters with a default cannot use forward declared identifiers
--> $DIR/issue-18183.rs:1:20
|
LL | pub struct Foo<Bar=Bar>(Bar);
| ^^^ defaulted type parameters cannot be forward declared
| ^^^ defaulted generic parameters cannot be forward declared
error: aborting due to previous error
......
error[E0128]: type parameters with a default cannot use forward declared identifiers
error[E0128]: generic parameters with a default cannot use forward declared identifiers
--> $DIR/issue-26812.rs:3:10
|
LL | fn avg<T=T::Item>(_: T) {}
| ^^^^^^^ defaulted type parameters cannot be forward declared
| ^^^^^^^ defaulted generic parameters cannot be forward declared
error: aborting due to previous error
......
......@@ -10,10 +10,10 @@ help: you can use `as` to change the binding name of the import
LL | extern crate core as other_core;
|
error: language item required, but not found: `eh_personality`
error: `#[panic_handler]` function required, but not found
error: language item required, but not found: `eh_personality`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0259`.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册