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());
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 => {
let mut err = struct_span_err!(
self.session,

View File

@ -8,9 +8,9 @@ Type parameter defaults can only use parameters that occur before them.
Erroneous code example:
```compile_fail,E0128
struct Foo<T=U, U=()> {
struct Foo<T = U, U = ()> {
field1: T,
filed2: U,
field2: U,
}
// error: type parameters with a default cannot use forward declared
// identifiers
@ -20,9 +20,9 @@ Since type parameters are evaluated in-order, you may be able to fix this issue
by doing:
```
struct Foo<U=(), T=U> {
struct Foo<U = (), T = U> {
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
// E0157, unused error code

View File

@ -214,6 +214,8 @@ enum ResolutionError<'a> {
BindingShadowsSomethingUnacceptable(&'a str, Name, &'a NameBinding<'a>),
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
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.
ConstParamDependentOnTypeParam,
}
@ -1536,7 +1538,7 @@ impl<'a> Resolver<'a> {
if let Some(res) = ribs[i].bindings.get(&rib_ident).cloned() {
// The ident resolves to a type parameter or local variable.
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(
&mut self,
rib_index: usize,
rib_ident: Ident,
res: Res,
record_used: bool,
span: Span,
@ -2133,7 +2136,12 @@ impl<'a> Resolver<'a> {
// An invalid forward use of a type parameter from a previous default.
if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind {
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);
return Res::Err;