From 53908b0790d318a4ef5de36f20a4c9c45a942d6e Mon Sep 17 00:00:00 2001 From: Leaf Petersen Date: Fri, 15 Dec 2017 18:12:03 +0000 Subject: [PATCH] Remove warning for "is" checks with generic type parameters. The analyzer used to emit a warning when a type parameter to a generic method was used in an "is" check. This warning was there to help users write code that worked correctly on both Dart 1.0 and 2.0. Now that 2.0 generic methods are being more broadly supported and used, this warning is blocking further library work, and is being removed. Fixes https://github.com/dart-lang/sdk/issues/30530 Bug: Change-Id: I70395305ad082aee3072b5beeb0b1b7f7883391b Reviewed-on: https://dart-review.googlesource.com/29821 Reviewed-by: Brian Wilkerson --- CHANGELOG.md | 10 +++++ pkg/analyzer/lib/error/error.dart | 1 - pkg/analyzer/lib/src/error/codes.dart | 13 ------- .../lib/src/generated/error_verifier.dart | 23 ----------- .../static_warning_code_kernel_test.dart | 21 ---------- .../generated/static_warning_code_test.dart | 39 ------------------- .../test/generated/strong_mode_test.dart | 3 -- tests/language/language_analyzer2.status | 1 + .../generic_methods_type_expression_test.dart | 5 +-- tests/language_2/language_2_analyzer.status | 14 ------- tests/language_2/language_2_dartdevc.status | 6 --- tests/language_2/language_2_kernel.status | 3 -- .../language_2/language_2_precompiled.status | 1 - tests/language_2/language_2_vm.status | 2 - 14 files changed, 13 insertions(+), 129 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f992918fa6..e0d48cbd69b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -111,6 +111,16 @@ ### Tool Changes +* Analyzer + +The analyzer will no longer issue a warning when a generic type parameter is +used as the type in an instance check. For example: + ```dart + test() { + print(3 is T); // No warning + } + ``` + * Pub * Git dependencies may now include a `path` parameter, indicating that the diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart index 4522042274c..34148ffe9ed 100644 --- a/pkg/analyzer/lib/error/error.dart +++ b/pkg/analyzer/lib/error/error.dart @@ -610,7 +610,6 @@ const List errorCodeValues = const [ StaticWarningCode.STATIC_ACCESS_TO_INSTANCE_MEMBER, StaticWarningCode.SWITCH_EXPRESSION_NOT_ASSIGNABLE, StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS, - StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER, StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, StaticWarningCode.TYPE_TEST_WITH_NON_TYPE, StaticWarningCode.TYPE_TEST_WITH_UNDEFINED_NAME, diff --git a/pkg/analyzer/lib/src/error/codes.dart b/pkg/analyzer/lib/src/error/codes.dart index 8c5e9eb9239..6241319b26a 100644 --- a/pkg/analyzer/lib/src/error/codes.dart +++ b/pkg/analyzer/lib/src/error/codes.dart @@ -4533,19 +4533,6 @@ class StaticWarningCode extends ErrorCode { "Try using a different type, or " "changing the import to not be deferred."); - /** - * Not yet spec'd. - * - * Parameters: - * 0: the name of the generic function's type parameter that is being used in - * an `is` expression - */ - static const StaticWarningCode TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER = - const StaticWarningCode( - 'TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER', - "The type parameter '{0}' can't be used in a type test.", - "Try using a different type."); - /** * 12.31 Type Test: It is a static warning if T does not denote a type * available in the current lexical scope. diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 3ce047a86bf..a0b598b618e 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -919,7 +919,6 @@ class ErrorVerifier extends RecursiveAstVisitor { @override Object visitIsExpression(IsExpression node) { _checkForTypeAnnotationDeferredClass(node.type); - _checkForTypeAnnotationGenericFunctionParameter(node.type); return super.visitIsExpression(node); } @@ -5766,28 +5765,6 @@ class ErrorVerifier extends RecursiveAstVisitor { } } - /** - * Verify that the given type [name] is not a type parameter in a generic - * method. - * - * See [StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER]. - */ - void _checkForTypeAnnotationGenericFunctionParameter(TypeAnnotation type) { - if (type is TypeName) { - Identifier name = type.name; - if (name is SimpleIdentifier) { - Element element = name.staticElement; - if (element is TypeParameterElement && - element.enclosingElement is ExecutableElement) { - _errorReporter.reportErrorForNode( - StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER, - name, - [name.name]); - } - } - } - } - /** * Verify that the type arguments in the given [typeName] are all within * their bounds. diff --git a/pkg/analyzer/test/generated/static_warning_code_kernel_test.dart b/pkg/analyzer/test/generated/static_warning_code_kernel_test.dart index 018a640cb9b..aa9081da400 100644 --- a/pkg/analyzer/test/generated/static_warning_code_kernel_test.dart +++ b/pkg/analyzer/test/generated/static_warning_code_kernel_test.dart @@ -1737,27 +1737,6 @@ class StaticWarningCodeTest_Kernel extends StaticWarningCodeTest_Driver { return super.test_typeAnnotationDeferredClass_variableDeclarationList(); } - @override - @failingTest - @potentialAnalyzerProblem - test_typeAnnotationGenericFunctionParameter_localFunction() async { - return super.test_typeAnnotationGenericFunctionParameter_localFunction(); - } - - @override - @failingTest - @potentialAnalyzerProblem - test_typeAnnotationGenericFunctionParameter_method() async { - return super.test_typeAnnotationGenericFunctionParameter_method(); - } - - @override - @failingTest - @potentialAnalyzerProblem - test_typeAnnotationGenericFunctionParameter_topLevelFunction() async { - return super.test_typeAnnotationGenericFunctionParameter_topLevelFunction(); - } - @override @failingTest @potentialAnalyzerProblem diff --git a/pkg/analyzer/test/generated/static_warning_code_test.dart b/pkg/analyzer/test/generated/static_warning_code_test.dart index 02aac49c739..545499478e7 100644 --- a/pkg/analyzer/test/generated/static_warning_code_test.dart +++ b/pkg/analyzer/test/generated/static_warning_code_test.dart @@ -3389,45 +3389,6 @@ a.A v;''' ]); } - test_typeAnnotationGenericFunctionParameter_localFunction() async { - Source source = addSource(r''' -class A { - void method() { - T local(Object t) { - return (t is T) ? t : null; - } - } -}'''); - await computeAnalysisResult(source); - assertErrors( - source, [StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER]); - verify([source]); - } - - test_typeAnnotationGenericFunctionParameter_method() async { - Source source = addSource(r''' -class A { - T method(Object t) { - return (t is T) ? t : null; - } -}'''); - await computeAnalysisResult(source); - assertErrors( - source, [StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER]); - verify([source]); - } - - test_typeAnnotationGenericFunctionParameter_topLevelFunction() async { - Source source = addSource(r''' -T function(Object t) { - return (t is T) ? t : null; -}'''); - await computeAnalysisResult(source); - assertErrors( - source, [StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER]); - verify([source]); - } - test_typeParameterReferencedByStatic_field() async { Source source = addSource(r''' class A { diff --git a/pkg/analyzer/test/generated/strong_mode_test.dart b/pkg/analyzer/test/generated/strong_mode_test.dart index b6a7b9afc38..d508efa8fa3 100644 --- a/pkg/analyzer/test/generated/strong_mode_test.dart +++ b/pkg/analyzer/test/generated/strong_mode_test.dart @@ -2796,12 +2796,9 @@ main() { await resolveTestUnit(code); } - @failingTest test_futureOr_promotion3() async { // Test that promotion from FutureOr to T works for type // parameters T - // TODO(leafp): When the restriction on is checks for generic methods - // goes away this should pass. String code = r''' import "dart:async"; dynamic test(FutureOr x) => (x is T) && diff --git a/tests/language/language_analyzer2.status b/tests/language/language_analyzer2.status index 36bd44f77ed..d2c1b6ff339 100644 --- a/tests/language/language_analyzer2.status +++ b/tests/language/language_analyzer2.status @@ -105,6 +105,7 @@ generic_closure_test: StaticWarning generic_list_checked_test: StaticWarning generic_local_functions_test: CompileTimeError # Issue 28515 generic_methods_generic_function_parameter_test: CompileTimeError # Issue 28515 +generic_methods_type_expression_test: Skip # This tests now unsupported Dart 1.0 behavior (is check warnings) generic_test: StaticWarning generics_test: StaticWarning get_set_syntax_test/none: Fail # Issue 11575 diff --git a/tests/language_2/generic_methods_type_expression_test.dart b/tests/language_2/generic_methods_type_expression_test.dart index 06c54eb0437..c83b20a2648 100644 --- a/tests/language_2/generic_methods_type_expression_test.dart +++ b/tests/language_2/generic_methods_type_expression_test.dart @@ -54,10 +54,9 @@ main() { Expect.equals(f8(), new TypeValue>().value); Expect.isTrue(f9({})); - Expect - .isTrue(f9({})); // `is Map` is true. + Expect.isFalse(f9({})); Expect.isFalse(f9({})); Expect.isTrue(new IsMap().check({})); - Expect.isTrue(new IsMap().check({})); + Expect.isFalse(new IsMap().check({})); } diff --git a/tests/language_2/language_2_analyzer.status b/tests/language_2/language_2_analyzer.status index 6ccda0b46d0..e93ae0defd5 100644 --- a/tests/language_2/language_2_analyzer.status +++ b/tests/language_2/language_2_analyzer.status @@ -325,16 +325,12 @@ function_type_call_getter2_test/04: MissingCompileTimeError function_type_call_getter2_test/05: MissingCompileTimeError generic_list_checked_test: CompileTimeError generic_methods_bounds_test/01: MissingCompileTimeError -generic_methods_closure_test: StaticWarning generic_methods_dynamic_test/01: MissingCompileTimeError generic_methods_dynamic_test/03: MissingCompileTimeError -generic_methods_local_variable_declaration_test: StaticWarning generic_methods_overriding_test/01: MissingCompileTimeError generic_methods_overriding_test/03: MissingCompileTimeError generic_methods_overriding_test/06: StaticWarning generic_methods_recursive_bound_test/02: MissingCompileTimeError -generic_methods_shadowing_test: StaticWarning -generic_methods_simple_is_expression_test: StaticWarning generic_no_such_method_dispatcher_test: StaticWarning generic_tearoff_test: CompileTimeError generic_test: CompileTimeError @@ -741,16 +737,12 @@ function_type_call_getter2_test/03: MissingCompileTimeError function_type_call_getter2_test/04: MissingCompileTimeError function_type_call_getter2_test/05: MissingCompileTimeError generic_methods_bounds_test/01: MissingCompileTimeError -generic_methods_closure_test: StaticWarning generic_methods_dynamic_test/01: MissingCompileTimeError generic_methods_dynamic_test/03: MissingCompileTimeError -generic_methods_local_variable_declaration_test: StaticWarning generic_methods_overriding_test/01: MissingCompileTimeError generic_methods_overriding_test/03: MissingCompileTimeError generic_methods_overriding_test/06: StaticWarning generic_methods_recursive_bound_test/02: MissingCompileTimeError -generic_methods_shadowing_test: StaticWarning -generic_methods_simple_is_expression_test: StaticWarning generic_no_such_method_dispatcher_test: StaticWarning generic_tearoff_test: CompileTimeError getter_no_setter2_test/00: MissingCompileTimeError @@ -1053,14 +1045,9 @@ error_stacktrace_test/00: Pass field3a_negative_test: StaticWarning # Issue 28823 forwarding_stub_tearoff_test: CompileTimeError generic_list_checked_test: CompileTimeError -generic_methods_closure_test: CompileTimeError # Issue 29070 generic_methods_generic_function_result_test/none: CompileTimeError # Issue #30207 -generic_methods_local_variable_declaration_test: CompileTimeError # Issue 29070 generic_methods_overriding_test/01: MissingCompileTimeError # Issue 29070 generic_methods_overriding_test/03: MissingCompileTimeError # Issue 29070 -generic_methods_shadowing_test: CompileTimeError # Issue 29070 -generic_methods_simple_is_expression_test: CompileTimeError # Issue 29070 -generic_methods_type_expression_test: CompileTimeError # Incorrectly disallows type parameter in "is" test. generic_no_such_method_dispatcher_test: CompileTimeError generic_tearoff_test: CompileTimeError generic_test: CompileTimeError @@ -1333,7 +1320,6 @@ generic_constructor_mixin_test/01: MissingCompileTimeError generic_field_mixin6_test/01: MissingCompileTimeError generic_function_typedef2_test/04: MissingCompileTimeError generic_methods_generic_function_result_test/01: MissingCompileTimeError # Issue #30207 -generic_methods_type_expression_test: StaticWarning # Issue 30530 identical_const_test/01: MissingCompileTimeError identical_const_test/02: MissingCompileTimeError identical_const_test/03: MissingCompileTimeError diff --git a/tests/language_2/language_2_dartdevc.status b/tests/language_2/language_2_dartdevc.status index 662f5bb64ea..3ae5df5291f 100644 --- a/tests/language_2/language_2_dartdevc.status +++ b/tests/language_2/language_2_dartdevc.status @@ -48,13 +48,8 @@ final_syntax_test/04: MissingCompileTimeError forwarding_stub_tearoff_test: CompileTimeError fuzzy_arrows_test/01: MissingCompileTimeError generic_local_functions_test: CompileTimeError -generic_methods_closure_test: CompileTimeError # Issue 29920 generic_methods_generic_function_parameter_test: CompileTimeError generic_methods_generic_function_result_test/none: CompileTimeError # Issue #30208 -generic_methods_local_variable_declaration_test: CompileTimeError # Issue 29920 -generic_methods_shadowing_test: CompileTimeError # Issue 29920 -generic_methods_simple_is_expression_test: CompileTimeError # Issue 29920 -generic_methods_type_expression_test: CompileTimeError generic_no_such_method_dispatcher_simple_test: Skip # This test is just for kernel. generic_no_such_method_dispatcher_test: CompileTimeError generic_test: CompileTimeError @@ -790,7 +785,6 @@ generic_closure_test/01: RuntimeError # ReferenceError: TToT is not defined generic_closure_test/none: RuntimeError # ReferenceError: TToT is not defined generic_list_checked_test: RuntimeError # Expect.throws fails: Did not throw generic_method_types_test/02: RuntimeError -generic_methods_type_expression_test: RuntimeError # Expect.isTrue(false) fails. generic_methods_unused_parameter_test: RuntimeError # Expect.isTrue(false) fails. generic_test: RuntimeError # ReferenceError: BOfT is not defined library_env_test/has_io_support: RuntimeError # Unsupported operation: bool.fromEnvironment can only be used as a const constructor diff --git a/tests/language_2/language_2_kernel.status b/tests/language_2/language_2_kernel.status index 0d8d7da3eeb..2e049eddfae 100644 --- a/tests/language_2/language_2_kernel.status +++ b/tests/language_2/language_2_kernel.status @@ -664,7 +664,6 @@ generic_methods_overriding_test/03: MissingCompileTimeError generic_methods_recursive_bound_test/02: MissingCompileTimeError generic_methods_tearoff_specialization_test: CompileTimeError # Issue 31402 (Variable declaration) generic_methods_tearoff_specialization_test: RuntimeError -generic_methods_type_expression_test: RuntimeError # Issue 25869 / 27460 generic_methods_unused_parameter_test: CompileTimeError # Issue 31402 (Variable declaration) generic_methods_unused_parameter_test: RuntimeError generic_no_such_method_dispatcher_simple_test: CompileTimeError # Issue 31533 @@ -1916,8 +1915,6 @@ generic_methods_recursive_bound_test/03: Crash, Pass generic_methods_reuse_type_variables_test: Pass generic_methods_tearoff_specialization_test: RuntimeError generic_methods_tearoff_specialization_test: CompileTimeError # Issue 31402 (Variable declaration) -generic_methods_type_expression_test: Crash -generic_methods_type_expression_test: RuntimeError # Issue 25869 / 27460 generic_methods_unused_parameter_test: CompileTimeError # Issue 31402 (Variable declaration) generic_methods_unused_parameter_test: RuntimeError generic_no_such_method_dispatcher_simple_test: CompileTimeError # Issue 31533 diff --git a/tests/language_2/language_2_precompiled.status b/tests/language_2/language_2_precompiled.status index 21dc489ad38..730698d4053 100644 --- a/tests/language_2/language_2_precompiled.status +++ b/tests/language_2/language_2_precompiled.status @@ -405,7 +405,6 @@ generic_methods_recursive_bound_test/02: Crash generic_methods_recursive_bound_test/02: MissingCompileTimeError generic_methods_recursive_bound_test/03: Crash, Pass generic_methods_tearoff_specialization_test: RuntimeError -generic_methods_type_expression_test: RuntimeError # Issue 25869 / 27460 generic_methods_unused_parameter_test: RuntimeError generic_tearoff_test: RuntimeError getter_no_setter2_test/00: MissingCompileTimeError diff --git a/tests/language_2/language_2_vm.status b/tests/language_2/language_2_vm.status index d4c9b9dd790..2952f762dca 100644 --- a/tests/language_2/language_2_vm.status +++ b/tests/language_2/language_2_vm.status @@ -43,7 +43,6 @@ example_constructor_test: Fail, OK export_ambiguous_main_negative_test: Fail # Issue 14763 field_initialization_order_test: Fail, OK generalized_void_syntax_test: CompileTimeError # Issue #30176 -generic_methods_type_expression_test: RuntimeError # Issue 25869 / 27460 hello_dart_test: Skip # Incompatible flag: --compile_all language_2/least_upper_bound_expansive_test/none: CompileTimeError library_env_test/has_html_support: RuntimeError, OK @@ -1185,7 +1184,6 @@ export_ambiguous_main_negative_test: Fail # Issue 14763 field_initialization_order_test: Fail, OK generalized_void_syntax_test: CompileTimeError # Issue #30176 generic_methods_bounds_test/02: MissingRuntimeError -generic_methods_type_expression_test: RuntimeError # Issue 25869 / 27460 library_env_test/has_html_support: RuntimeError, OK library_env_test/has_no_io_support: RuntimeError, OK main_not_a_function_test: Skip