Skip to content

Commit

Permalink
Version 3.5.0-145.0.dev
Browse files Browse the repository at this point in the history
Merge babd21e into dev
  • Loading branch information
Dart CI committed May 11, 2024
2 parents de30a5d + babd21e commit 03bc3cd
Show file tree
Hide file tree
Showing 24 changed files with 383 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,8 @@ CompileTimeErrorCode.MACRO_ERROR:
status: noFix
CompileTimeErrorCode.MACRO_INTERNAL_EXCEPTION:
status: noFix
CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION:
status: noFix
CompileTimeErrorCode.MAIN_FIRST_POSITIONAL_PARAMETER_TYPE:
status: needsFix
notes: |-
Expand Down
15 changes: 10 additions & 5 deletions pkg/analyzer/lib/src/dart/analysis/file_state_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class _AnyFilter implements FileStateFilter {
class _PubFilter implements FileStateFilter {
final PubPackage targetPackage;
final String? targetPackageName;
final bool targetPackageIsAnalysisServer;

/// "Friends of `package:analyzer` see analyzer implementation libraries in
/// completions.
final bool targetPackageIsFriendOfAnalyzer;
final bool targetInLibOrEntryPoint;
final Set<String> dependencies;

Expand All @@ -84,7 +87,8 @@ class _PubFilter implements FileStateFilter {
return _PubFilter._(
targetPackage: package,
targetPackageName: packageName,
targetPackageIsAnalysisServer: packageName == 'analysis_server',
targetPackageIsFriendOfAnalyzer:
packageName == 'analysis_server' || packageName == 'linter',
targetInLibOrEntryPoint: inLibOrEntryPoint,
dependencies: dependencies,
);
Expand All @@ -93,7 +97,7 @@ class _PubFilter implements FileStateFilter {
_PubFilter._({
required this.targetPackage,
required this.targetPackageName,
required this.targetPackageIsAnalysisServer,
required this.targetPackageIsFriendOfAnalyzer,
required this.targetInLibOrEntryPoint,
required this.dependencies,
});
Expand Down Expand Up @@ -125,8 +129,9 @@ class _PubFilter implements FileStateFilter {

// If not the same package, must be public.
if (uri.isSrc) {
// Special case `analysis_server` access to `analyzer`.
if (targetPackageIsAnalysisServer && packageName == 'analyzer') {
// Special case access to `analyzer` to allow privileged access
// from "friends" like `analysis_server` and `linter`.
if (targetPackageIsFriendOfAnalyzer && packageName == 'analyzer') {
return true;
}
return false;
Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3162,6 +3162,18 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
"{0} {1}",
);

/// Parameters:
/// 0: the macro phase
/// 1: the list of ranges in the code
/// 2: the generated code
static const CompileTimeErrorCode MACRO_NOT_ALLOWED_DECLARATION =
CompileTimeErrorCode(
'MACRO_NOT_ALLOWED_DECLARATION',
"The macro attempted to add declaration(s) not allowed during the {0} "
"phase.\nLocations: {1}\n---\n{2}\n---",
correctionMessage: "Try adding these declaration during an earlier phase.",
);

/// No parameters.
static const CompileTimeErrorCode MAIN_FIRST_POSITIONAL_PARAMETER_TYPE =
CompileTimeErrorCode(
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.MACRO_DEFINITION_APPLICATION_SAME_LIBRARY_CYCLE,
CompileTimeErrorCode.MACRO_ERROR,
CompileTimeErrorCode.MACRO_INTERNAL_EXCEPTION,
CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION,
CompileTimeErrorCode.MAIN_FIRST_POSITIONAL_PARAMETER_TYPE,
CompileTimeErrorCode.MAIN_HAS_REQUIRED_NAMED_PARAMETERS,
CompileTimeErrorCode.MAIN_HAS_TOO_MANY_REQUIRED_POSITIONAL_PARAMETERS,
Expand Down
13 changes: 11 additions & 2 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6645,8 +6645,17 @@ class _MacroDiagnosticsReporter {
void _reportNotAllowedDeclaration(
NotAllowedDeclarationDiagnostic diagnostic,
) {
// TODO(scheglov): implement
throw UnimplementedError();
errorReporter.atNode(
element.annotationAst(diagnostic.annotationIndex),
CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION,
arguments: [
diagnostic.phase.name,
diagnostic.nodeRanges
.map((r) => '(${r.offset}, ${r.length})')
.join(' '),
diagnostic.code.trimRight(),
],
);
}

static SimpleIdentifier _annotationNameIdentifier(
Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/lib/src/summary2/library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ class LibraryBuilder with MacroApplicationsContainer {
required ElementImpl? targetElement,
required OperationPerformanceImpl performance,
}) async {
if (!element.featureSet.isEnabled(Feature.macros)) {
return MacroDeclarationsPhaseStepResult.nothing;
}

var macroApplier = linker.macroApplier;
if (macroApplier == null) {
return MacroDeclarationsPhaseStepResult.nothing;
Expand Down Expand Up @@ -373,6 +377,10 @@ class LibraryBuilder with MacroApplicationsContainer {
Future<void> executeMacroDefinitionsPhase({
required OperationPerformanceImpl performance,
}) async {
if (!element.featureSet.isEnabled(Feature.macros)) {
return;
}

var macroApplier = linker.macroApplier;
if (macroApplier == null) {
return;
Expand Down Expand Up @@ -410,6 +418,10 @@ class LibraryBuilder with MacroApplicationsContainer {
Future<void> executeMacroTypesPhase({
required OperationPerformanceImpl performance,
}) async {
if (!element.featureSet.isEnabled(Feature.macros)) {
return;
}

var macroApplier = linker.macroApplier;
if (macroApplier == null) {
return;
Expand Down
5 changes: 2 additions & 3 deletions pkg/analyzer/lib/src/utilities/extensions/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ extension IterableExtension<E> on Iterable<E> {

/// Returns the fixed-length [List] with elements of `this`.
List<E> toFixedList() {
var result = toList(growable: false);
if (result.isEmpty) {
if (isEmpty) {
return const <Never>[];
}
return result;
return toList(growable: false);
}

Iterable<E> whereNotType<U>() {
Expand Down
14 changes: 14 additions & 0 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9675,6 +9675,20 @@ CompileTimeErrorCode:
Parameters:
0: the message
1: the stack trace
MACRO_NOT_ALLOWED_DECLARATION:
problemMessage: |-
The macro attempted to add declaration(s) not allowed during the {0} phase.
Locations: {1}
---
{2}
---
correctionMessage: Try adding these declaration during an earlier phase.
hasPublishedDocs: false
comment: |-
Parameters:
0: the macro phase
1: the list of ranges in the code
2: the generated code
MACRO_DECLARATIONS_PHASE_INTROSPECTION_CYCLE:
problemMessage: "The declaration '{0}' can't be introspected because there is a cycle of macro applications."
correctionMessage: Try removing one or more macro applications to break the cycle.
Expand Down
13 changes: 13 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ import 'diagnostic.dart';
]);
}

test_diagnostic_notAllowedDeclaration_declarations_class() async {
await assertErrorsInCode('''
import 'append.dart';
class A {
@DeclareInLibrary('class B {}')
void foo() {}
}
''', [
error(CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION, 35, 31),
]);
}

test_diagnostic_notSupportedArgument() async {
await assertErrorsInCode('''
import 'diagnostic.dart';
Expand Down
71 changes: 71 additions & 0 deletions pkg/analyzer/test/src/summary/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5724,6 +5724,77 @@ abstract class MacroElementsTest extends MacroElementsBaseTest {
@override
bool get retainDataForTesting => true;

test_disable_declarationsPhase() async {
var library = await buildLibrary(r'''
// @dart = 3.2
import 'append.dart';
@DeclareInType(' void foo() {}')
class A {}
''');

configuration
..withConstructors = false
..withMetadata = false;
checkElementText(library, r'''
library
imports
package:test/append.dart
definingUnit
classes
class A @78
''');
}

test_disable_definitionsPhase() async {
var library = await buildLibrary(r'''
// @dart = 3.2
import 'append.dart';
class A {
@AugmentDefinition('{ print(0); }')
void foo() {}
}
''');

configuration
..withConstructors = false
..withMetadata = false;
checkElementText(library, r'''
library
imports
package:test/append.dart
definingUnit
classes
class A @44
methods
foo @93
returnType: void
''');
}

test_disable_typesPhase() async {
var library = await buildLibrary(r'''
// @dart = 3.2
import 'append.dart';
@DeclareType('B', 'class B {}')
class A {}
''');

configuration
..withConstructors = false
..withMetadata = false;
checkElementText(library, r'''
library
imports
package:test/append.dart
definingUnit
classes
class A @76
''');
}

test_macroApplicationErrors_typesPhase_compileTimeError() async {
newFile('$testPackageLibPath/a.dart', r'''
import 'package:macros/macros.dart';
Expand Down
13 changes: 11 additions & 2 deletions pkg/dartdev/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ Run "${runner!.executableName} help" to see global options.''');
.where((e) => !e.startsWith('--$experimentFlagName='))
.toList();
log.trace('dart $testExecutable ${argsRestNoExperiment.join(' ')}');
VmInteropHandler.run(testExecutable.executable, argsRestNoExperiment,
packageConfigOverride: testExecutable.packageConfig!);
VmInteropHandler.run(
testExecutable.executable,
argsRestNoExperiment,
packageConfigOverride: testExecutable.packageConfig!,
// TODO(bkonyi): remove once DartDev moves to AOT and this flag can be
// provided directly to the process spawned by `dart run` and
// `dart test`.
//
// See https://github.com/dart-lang/sdk/issues/53576
markMainIsolateAsSystemIsolate: true,
);
return 0;
} on CommandResolutionFailedException catch (e) {
if (project.hasPubspecFile) {
Expand Down
12 changes: 8 additions & 4 deletions pkg/dartdev/lib/src/vm_interop_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ abstract class VmInteropHandler {
///
/// If [packageConfigOverride] is given, that is where the packageConfig is found.
///
/// If [forceNoSoundNullSafety] is given and set to true, the spawned isolate will run
/// with `--no-sound-null-safety` enabled.
/// If [markMainIsolateAsSystemIsolate] is given and set to true, the spawned
/// isolate will run with `--mark-main-isolate-as-system-isolate` enabled.
static void run(
String script,
List<String> args, {
String? packageConfigOverride,
bool forceNoSoundNullSafety = false,
// TODO(bkonyi): remove once DartDev moves to AOT and this flag can be
// provided directly to the process spawned by `dart run` and `dart test`.
//
// See https://github.com/dart-lang/sdk/issues/53576
bool markMainIsolateAsSystemIsolate = false,
}) {
final port = _port;
if (port == null) return;
final message = <dynamic>[
_kResultRun,
script,
packageConfigOverride,
forceNoSoundNullSafety,
markMainIsolateAsSystemIsolate,
// Copy the list so it doesn't get GC'd underneath us.
args.toList()
];
Expand Down
50 changes: 50 additions & 0 deletions pkg/dartdev/test/commands/test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// 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 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';
import 'package:vm_service/vm_service_io.dart';

import '../experiment_util.dart';
import '../utils.dart';
Expand Down Expand Up @@ -157,6 +160,53 @@ void main() {
expect(result.stderr, isEmpty);
});

test('implicitly passes --mark-main-isolate-as-system-isolate', () async {
// --mark-main-isolate-as-system-isolate is necessary for DevTools to be
// able to identify the correct root library.
//
// See https://github.com/flutter/flutter/issues/143170 for details.
final p = project(
mainSrc: 'int get foo => 1;\n',
pubspecExtras: {
'dev_dependencies': {'test': 'any'}
},
);
p.file('test/foo_test.dart', '''
import 'package:test/test.dart';

void main() {
test('', () {
print('hello world');
});
}
''');

final vmServiceUriRegExp =
RegExp(r'(http:\/\/127.0.0.1:\d*\/[\da-zA-Z-_]*=\/)');
final process = await p.start(['test', '--pause-after-load']);
final completer = Completer<Uri>();
late StreamSubscription sub;
sub = process.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((line) async {
if (line.contains(vmServiceUriRegExp)) {
await sub.cancel();
final httpUri = Uri.parse(
vmServiceUriRegExp.firstMatch(line)!.group(0)!,
);
completer.complete(
httpUri.replace(scheme: 'ws', path: '${httpUri.path}ws'),
);
}
});

final vmServiceUri = await completer.future;
final vmService = await vmServiceConnectUri(vmServiceUri.toString());
final vm = await vmService.getVM();
expect(vm.systemIsolates!.where((e) => e.name == 'main'), isNotEmpty);
});

group('--enable-experiment', () {
late TestProject p;
Future<ProcessResult> runTestWithExperimentFlag(String? flag) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,11 @@ const AllowedExperimentalFlags defaultAllowedExperimentalFlags =
const AllowedExperimentalFlags(
sdkDefaultExperiments: {},
sdkLibraryExperiments: {},
packageExperiments: {});
packageExperiments: {
"json": {
ExperimentalFlag.macros,
},
});
const Map<shared.ExperimentalFlag, ExperimentalFlag> sharedExperimentalFlags = {
shared.ExperimentalFlag.classModifiers: ExperimentalFlag.classModifiers,
shared.ExperimentalFlag.constFunctions: ExperimentalFlag.constFunctions,
Expand Down
Loading

0 comments on commit 03bc3cd

Please sign in to comment.