From a18d424c48e5b2e23cebf5ab5969a3254ab90a9e Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 27 Sep 2019 15:21:02 +0200 Subject: [PATCH] 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. --- src/librustc_resolve/diagnostics.rs | 11 +++++++++++ src/librustc_resolve/error_codes.rs | 23 +++++++++++++++++++---- src/librustc_resolve/lib.rs | 12 ++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index c479912b4ef..7f819486f5b 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -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, diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index 9a39fcf4223..47346774180 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -8,9 +8,9 @@ Type parameter defaults can only use parameters that occur before them. Erroneous code example: ```compile_fail,E0128 -struct Foo { +struct Foo { 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 { +struct Foo { field1: T, - filed2: U, + field2: U, } ``` @@ -1705,6 +1705,21 @@ fn const_id() -> 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> { + field1: Option, + field2: Option, +} +// error: type parameters cannot use `Self` in their defaults. +``` +"##, + ; // E0153, unused error code // E0157, unused error code diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index bf86a374338..e7292b52ab3 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -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;