Skip to content

Commit

Permalink
Warn about deprecated native extensions
Browse files Browse the repository at this point in the history
Fixes dart-lang#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>
  • Loading branch information
srawlins authored and commit-bot@chromium.org committed May 21, 2021
1 parent 2b507fb commit 5d40322
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/analyzer/lib/error/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions pkg/analyzer/lib/src/dart/error/hint_codes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/lib/src/error/best_practices_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
void visitExportDirective(ExportDirective node) {
_deprecatedVerifier.exportDirective(node);
_checkForInternalExport(node);
_checkForUseOfNativeExtension(node);
super.visitExportDirective(node);
}

Expand Down Expand Up @@ -521,6 +522,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
}
_invalidAccessVerifier.verifyImport(node);
_checkForImportOfLegacyLibraryIntoNullSafe(node);
_checkForUseOfNativeExtension(node);
super.visitImportDirective(node);
}

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions pkg/analyzer/test/generated/non_error_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/analyzer/test/src/diagnostics/test_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 12 additions & 6 deletions pkg/analyzer/test/src/diagnostics/uri_does_not_exist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}
}
Original file line number Diff line number Diff line change
@@ -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),
]);
}
}

0 comments on commit 5d40322

Please sign in to comment.