Allocate a new diagnostic for defaulted type parameters cannot use `Self`

(Without this commit, you still get an error (a very similar one, at
that), but it complains about use of forward declaration, which is
confusing since people do not necessarily think of `Self` as being
declared at all.)

Update: incorporate review feedback.
This commit is contained in:
Felix S. Klock II 2019-09-27 15:21:02 +02:00
parent 3a4921cde1
commit a18d424c48
3 changed files with 40 additions and 6 deletions

View File

@ -354,6 +354,17 @@ impl<'a> Resolver<'a> {
span, "defaulted type parameters cannot be forward declared".to_string()); span, "defaulted type parameters cannot be forward declared".to_string());
err err
} }
ResolutionError::SelfInTyParamDefault => {
let mut err = struct_span_err!(
self.session,
span,
E0735,
"type parameters cannot use `Self` in their defaults"
);
err.span_label(
span, "`Self` in type parameter default".to_string());
err
}
ResolutionError::ConstParamDependentOnTypeParam => { ResolutionError::ConstParamDependentOnTypeParam => {
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.session, self.session,

View File

@ -8,9 +8,9 @@ Type parameter defaults can only use parameters that occur before them.
Erroneous code example: Erroneous code example:
```compile_fail,E0128 ```compile_fail,E0128
struct Foo<T=U, U=()> { struct Foo<T = U, U = ()> {
field1: T, field1: T,
filed2: U, field2: U,
} }
// error: type parameters with a default cannot use forward declared // error: type parameters with a default cannot use forward declared
// identifiers // identifiers
@ -20,9 +20,9 @@ Since type parameters are evaluated in-order, you may be able to fix this issue
by doing: by doing:
``` ```
struct Foo<U=(), T=U> { struct Foo<U = (), T = U> {
field1: T, field1: T,
filed2: U, field2: U,
} }
``` ```
@ -1705,6 +1705,21 @@ fn const_id<T, const N: T>() -> T { // error: const parameter
} }
``` ```
"##, "##,
E0735: r##"
Type parameter defaults cannot use `Self` on structs, enums, or unions.
Erroneous code example:
```compile_fail,E0735
struct Foo<X = Box<Self>> {
field1: Option<X>,
field2: Option<X>,
}
// error: type parameters cannot use `Self` in their defaults.
```
"##,
; ;
// E0153, unused error code // E0153, unused error code
// E0157, unused error code // E0157, unused error code

View File

@ -214,6 +214,8 @@ enum ResolutionError<'a> {
BindingShadowsSomethingUnacceptable(&'a str, Name, &'a NameBinding<'a>), BindingShadowsSomethingUnacceptable(&'a str, Name, &'a NameBinding<'a>),
/// Error E0128: type parameters with a default cannot use forward-declared identifiers. /// Error E0128: type parameters with a default cannot use forward-declared identifiers.
ForwardDeclaredTyParam, // FIXME(const_generics:defaults) ForwardDeclaredTyParam, // FIXME(const_generics:defaults)
/// Error E0735: type parameters with a default cannot use `Self`
SelfInTyParamDefault,
/// Error E0671: const parameter cannot depend on type parameter. /// Error E0671: const parameter cannot depend on type parameter.
ConstParamDependentOnTypeParam, ConstParamDependentOnTypeParam,
} }
@ -1536,7 +1538,7 @@ impl<'a> Resolver<'a> {
if let Some(res) = ribs[i].bindings.get(&rib_ident).cloned() { if let Some(res) = ribs[i].bindings.get(&rib_ident).cloned() {
// The ident resolves to a type parameter or local variable. // The ident resolves to a type parameter or local variable.
return Some(LexicalScopeBinding::Res( return Some(LexicalScopeBinding::Res(
self.validate_res_from_ribs(i, res, record_used, path_span, ribs), self.validate_res_from_ribs(i, rib_ident, res, record_used, path_span, ribs),
)); ));
} }
@ -2122,6 +2124,7 @@ impl<'a> Resolver<'a> {
fn validate_res_from_ribs( fn validate_res_from_ribs(
&mut self, &mut self,
rib_index: usize, rib_index: usize,
rib_ident: Ident,
res: Res, res: Res,
record_used: bool, record_used: bool,
span: Span, span: Span,
@ -2133,7 +2136,12 @@ impl<'a> Resolver<'a> {
// An invalid forward use of a type parameter from a previous default. // An invalid forward use of a type parameter from a previous default.
if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind { if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind {
if record_used { if record_used {
self.report_error(span, ResolutionError::ForwardDeclaredTyParam); let res_error = if rib_ident.name == kw::SelfUpper {
ResolutionError::SelfInTyParamDefault
} else {
ResolutionError::ForwardDeclaredTyParam
};
self.report_error(span, res_error);
} }
assert_eq!(res, Res::Err); assert_eq!(res, Res::Err);
return Res::Err; return Res::Err;