Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor authored and JulianKnodt committed Mar 23, 2021
1 parent e4e5db4 commit 8ef8138
Show file tree
Hide file tree
Showing 27 changed files with 74 additions and 89 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Expand Up @@ -1180,7 +1180,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
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;
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Expand Up @@ -2659,6 +2659,7 @@ impl<'a> State<'a> {
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();
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -963,9 +963,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.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))
}
})
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -1876,13 +1876,16 @@ impl EncodeContext<'a, '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)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_passes/src/stability.rs
Expand Up @@ -507,10 +507,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, '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,
};

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_privacy/src/lib.rs
Expand Up @@ -931,6 +931,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
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);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Expand Up @@ -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
}
Expand Down
28 changes: 14 additions & 14 deletions compiler/rustc_resolve/src/late.rs
Expand Up @@ -132,10 +132,10 @@ crate enum RibKind<'a> {
/// 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.
Expand All @@ -154,7 +154,7 @@ impl RibKind<'_> {
| ModuleRibKind(_)
| MacroDefinition(_)
| ConstParamTyRibKind => false,
AssocItemRibKind | ItemRibKind(_) | ForwardTyParamBanRibKind => true,
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
}
}
}
Expand Down Expand Up @@ -555,15 +555,16 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
// 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,
},
));

Expand Down Expand Up @@ -591,8 +592,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {

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.
//
Expand All @@ -616,7 +617,6 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
self.visit_ty(ty);
self.ribs[TypeNS].pop().unwrap();
self.ribs[ValueNS].pop().unwrap();
// FIXME(const_generics:default) do something with default here?
}
}
}
Expand Down Expand Up @@ -866,7 +866,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
| ItemRibKind(..)
| ConstantItemRibKind(..)
| ModuleRibKind(..)
| ForwardTyParamBanRibKind
| ForwardGenericParamBanRibKind
| ConstParamTyRibKind => {
return false;
}
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_resolve/src/lib.rs
Expand Up @@ -2592,8 +2592,8 @@ impl<'a> Resolver<'a> {
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
Expand All @@ -2617,7 +2617,7 @@ impl<'a> Resolver<'a> {
| ClosureOrAsyncRibKind
| ModuleRibKind(..)
| MacroDefinition(..)
| ForwardTyParamBanRibKind => {
| ForwardGenericParamBanRibKind => {
// Nothing to do. Continue.
}
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
Expand Down Expand Up @@ -2689,7 +2689,9 @@ impl<'a> Resolver<'a> {

// 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;
}
Expand Down Expand Up @@ -2782,7 +2784,9 @@ impl<'a> Resolver<'a> {

// 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;
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_typeck/src/astconv/mod.rs
Expand Up @@ -507,14 +507,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
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()
} else {
// We've already errored above about the mismatch.
tcx.const_error(ty).into()
}
// FIXME(const_generic_defaults)
/*
if !infer_args && has_default {
/*
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Expand Up @@ -1447,7 +1447,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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!()
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Expand Up @@ -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`.
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_typeck/src/collect.rs
Expand Up @@ -254,10 +254,14 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'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);
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/collect/type_of.rs
Expand Up @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/ui/const-generics/defaults/const-default.rs
Expand Up @@ -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();
Expand Down
@@ -1,7 +1,6 @@
#![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>() {}
@@ -1,12 +1,9 @@
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`.
@@ -1,2 +1,4 @@
trait Foo<const KIND: bool = true> {}
//~^ ERROR default values for const generic parameters are experimental

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0128.stderr
@@ -1,8 +1,8 @@
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

Expand Down

This file was deleted.

This file was deleted.

9 changes: 1 addition & 8 deletions src/test/ui/generics/generic-non-trailing-defaults.stderr
Expand Up @@ -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`.
@@ -1,8 +1,8 @@
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

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-18183.stderr
@@ -1,8 +1,8 @@
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

Expand Down

0 comments on commit 8ef8138

Please sign in to comment.