Skip to content

Commit

Permalink
Allocate a new diagnostic for defaulted type parameters cannot use `S…
Browse files Browse the repository at this point in the history
…elf`

(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.
  • Loading branch information
pnkfelix committed Oct 3, 2019
1 parent 3a4921c commit a18d424
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/librustc_resolve/diagnostics.rs
Expand Up @@ -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,
Expand Down
23 changes: 19 additions & 4 deletions src/librustc_resolve/error_codes.rs
Expand Up @@ -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
Expand All @@ -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,
}
```
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/librustc_resolve/lib.rs
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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),
));
}

Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit a18d424

Please sign in to comment.