Some refactoring
This commit is contained in:
parent
e4e5db4e42
commit
8ef81388e2
27 changed files with 74 additions and 89 deletions
|
@ -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 @@ 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.
|
||||
|
@ -154,7 +154,7 @@ impl RibKind<'_> {
|
|||
| ModuleRibKind(_)
|
||||
| MacroDefinition(_)
|
||||
| ConstParamTyRibKind => false,
|
||||
AssocItemRibKind | ItemRibKind(_) | ForwardTyParamBanRibKind => true,
|
||||
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
},
|
||||
));
|
||||
|
||||
|
@ -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.
|
||||
//
|
||||
|
@ -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?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -866,7 +866,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
| ItemRibKind(..)
|
||||
| ConstantItemRibKind(..)
|
||||
| ModuleRibKind(..)
|
||||
| ForwardTyParamBanRibKind
|
||||
| ForwardGenericParamBanRibKind
|
||||
| ConstParamTyRibKind => {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -2617,7 +2617,7 @@ impl<'a> Resolver<'a> {
|
|||
| ClosureOrAsyncRibKind
|
||||
| ModuleRibKind(..)
|
||||
| MacroDefinition(..)
|
||||
| ForwardTyParamBanRibKind => {
|
||||
| ForwardGenericParamBanRibKind => {
|
||||
// Nothing to do. Continue.
|
||||
}
|
||||
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue