Warn about deprecated native extensions

Fixes https://github.com/dart-lang/sdk/issues/45759

Change-Id: I0162708a33aebec59edf6e7d3eb14b08e639e0af
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/200924
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Sam Rawlins 2021-05-21 01:29:55 +00:00 committed by commit-bot@chromium.org
parent 2b507fb850
commit 5d4032282b
7 changed files with 78 additions and 10 deletions

View file

@ -610,6 +610,7 @@ const List<ErrorCode> errorCodeValues = [
HintCode.UNUSED_RESULT,
HintCode.UNUSED_RESULT_WITH_MESSAGE,
HintCode.UNUSED_SHOWN_NAME,
HintCode.USE_OF_NATIVE_EXTENSION,
LanguageCode.IMPLICIT_DYNAMIC_FIELD,
LanguageCode.IMPLICIT_DYNAMIC_FUNCTION,
LanguageCode.IMPLICIT_DYNAMIC_INVOKE,

View file

@ -3196,6 +3196,15 @@ class HintCode extends AnalyzerErrorCode {
correction: "Try removing the name from the list of shown members.",
hasPublishedDocs: true);
/**
* Users should not import or export Dart native extensions via 'dart-ext:'.
*/
static const HintCode USE_OF_NATIVE_EXTENSION = HintCode(
'USE_OF_NATIVE_EXTENSION',
"Dart native extensions are deprecated and will not be available in Dart "
"2.15",
correction: "Try using dart:ffi for C interop.");
/**
* Initialize a newly created error code to have the given [name]. The message
* associated with the error will be created from the given [message]

View file

@ -368,6 +368,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
void visitExportDirective(ExportDirective node) {
_deprecatedVerifier.exportDirective(node);
_checkForInternalExport(node);
_checkForUseOfNativeExtension(node);
super.visitExportDirective(node);
}
@ -521,6 +522,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
}
_invalidAccessVerifier.verifyImport(node);
_checkForImportOfLegacyLibraryIntoNullSafe(node);
_checkForUseOfNativeExtension(node);
super.visitImportDirective(node);
}
@ -1389,6 +1391,16 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
return false;
}
void _checkForUseOfNativeExtension(UriBasedDirective node) {
var uri = node.uriContent;
if (uri == null) {
return;
}
if (uri.startsWith('dart-ext:')) {
_errorReporter.reportErrorForNode(HintCode.USE_OF_NATIVE_EXTENSION, node);
}
}
void _checkRequiredParameter(FormalParameterList node) {
final requiredParameters =
node.parameters.where((p) => p.declaredElement?.hasRequired == true);

View file

@ -2421,15 +2421,18 @@ class Foo {
const factory Foo.foo() native 'Foo_Foo_foo';
}
''', [
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 20),
error(ParserErrorCode.CONST_CONSTRUCTOR_WITH_BODY, 47, 6),
]);
}
test_nativeFunctionBodyInNonSDKCode_function() async {
await assertNoErrorsInCode(r'''
await assertErrorsInCode(r'''
import 'dart-ext:x';
int m(a) native 'string';
''');
''', [
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 20),
]);
}
test_newWithAbstractClass_factory() async {

View file

@ -243,7 +243,7 @@ import 'import_deferred_library_with_load_function_test.dart'
as import_deferred_library_with_load_function;
import 'import_internal_library_test.dart' as import_internal_library;
import 'import_of_legacy_library_into_null_safe_test.dart'
as mport_of_legacy_library_into_null_safe;
as import_of_legacy_library_into_null_safe;
import 'import_of_non_library_test.dart' as import_of_non_library;
import 'inconsistent_case_expression_types_test.dart'
as inconsistent_case_expression_types;
@ -669,6 +669,7 @@ import 'unused_local_variable_test.dart' as unused_local_variable;
import 'unused_shown_name_test.dart' as unused_shown_name;
import 'uri_does_not_exist_test.dart' as uri_does_not_exist;
import 'uri_with_interpolation_test.dart' as uri_with_interpolation;
import 'use_of_native_extension_test.dart' as use_of_native_extension;
import 'use_of_nullable_value_test.dart' as use_of_nullable_value_test;
import 'use_of_void_result_test.dart' as use_of_void_result;
import 'variable_type_mismatch_test.dart' as variable_type_mismatch;
@ -848,7 +849,7 @@ main() {
implicit_this_reference_in_initializer.main();
import_deferred_library_with_load_function.main();
import_internal_library.main();
mport_of_legacy_library_into_null_safe.main();
import_of_legacy_library_into_null_safe.main();
import_of_non_library.main();
inconsistent_case_expression_types.main();
inconsistent_inheritance_getter_and_method.main();
@ -1139,6 +1140,7 @@ main() {
unused_shown_name.main();
uri_does_not_exist.main();
uri_with_interpolation.main();
use_of_native_extension.main();
use_of_nullable_value_test.main();
use_of_void_result.main();
variable_type_mismatch.main();

View file

@ -91,22 +91,28 @@ part 'unknown.dart';
test_valid_dll() async {
newFile("$testPackageLibPath/lib.dll");
await assertNoErrorsInCode('''
await assertErrorsInCode('''
import 'dart-ext:lib';
''');
''', [
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 22),
]);
}
test_valid_dylib() async {
newFile("$testPackageLibPath/lib.dylib");
await assertNoErrorsInCode('''
await assertErrorsInCode('''
import 'dart-ext:lib';
''');
''', [
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 22),
]);
}
test_valid_so() async {
newFile("$testPackageLibPath/lib.so");
await assertNoErrorsInCode('''
await assertErrorsInCode('''
import 'dart-ext:lib';
''');
''', [
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 22),
]);
}
}

View file

@ -0,0 +1,35 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UseOfNativeExtensionTest);
});
}
@reflectiveTest
class UseOfNativeExtensionTest extends PubPackageResolutionTest {
test_export() async {
await assertErrorsInCode(r'''
export 'dart-ext:x';
''', [
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 20),
error(CompileTimeErrorCode.URI_DOES_NOT_EXIST, 7, 12),
]);
}
test_import() async {
await assertErrorsInCode(r'''
import 'dart-ext:x';
''', [
// TODO(srawlins): Why does this file not have a URI_DOES_NOT_EXIST error?
error(HintCode.USE_OF_NATIVE_EXTENSION, 0, 20),
]);
}
}