Skip to content

Commit

Permalink
Version 3.4.0-114.0.dev
Browse files Browse the repository at this point in the history
Merge 402f856 into dev
  • Loading branch information
Dart CI committed Feb 6, 2024
2 parents 5dccf16 + 402f856 commit 29265c9
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pkg/_fe_analyzer_shared/lib/src/macros/executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ abstract class MacroExecutionResult implements Serializable {
/// All [Diagnostic]s reported as a result of executing a macro.
List<Diagnostic> get diagnostics;

/// If execution was stopped by an exception, the exception.
MacroException? get exception;

/// Any augmentations to enum values that should be applied to an enum as a
/// result of executing a macro, indexed by the identifier of the enum.
Map<Identifier, Iterable<DeclarationCode>> get enumValueAugmentations;
Expand Down
10 changes: 10 additions & 0 deletions pkg/_fe_analyzer_shared/lib/src/macros/executor/builder_impls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart

import '../api.dart';
import '../executor.dart';
import 'exception_impls.dart';
import 'response_impls.dart';

abstract class TypeBuilderBase implements TypePhaseIntrospector, Builder {
/// All the collected diagnostics for this builder.
final List<Diagnostic> _diagnostics = [];

/// If execution was stopped by an exception, the exception.
MacroExceptionImpl? _exception;

/// All the enum values to be added, indexed by the identifier for the
/// augmented enum declaration.
final Map<IdentifierImpl, List<DeclarationCode>> _enumValueAugmentations;
Expand Down Expand Up @@ -42,6 +46,7 @@ abstract class TypeBuilderBase implements TypePhaseIntrospector, Builder {
/// created by this builder.
MacroExecutionResult get result => new MacroExecutionResultImpl(
diagnostics: _diagnostics,
exception: _exception,
enumValueAugmentations: _enumValueAugmentations,
interfaceAugmentations: _interfaceAugmentations,
libraryAugmentations: _libraryAugmentations,
Expand All @@ -65,6 +70,11 @@ abstract class TypeBuilderBase implements TypePhaseIntrospector, Builder {
@override
void report(Diagnostic diagnostic) => _diagnostics.add(diagnostic);

void failWithException(MacroExceptionImpl exception) {
if (_exception != null) throw new StateError('Already set exception');
_exception = exception;
}

@override
Future<Identifier> resolveIdentifier(Uri library, String identifier) =>
// ignore: deprecated_member_use_from_same_package
Expand Down
37 changes: 28 additions & 9 deletions pkg/_fe_analyzer_shared/lib/src/macros/executor/execute_macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:_fe_analyzer_shared/src/macros/api.dart';
import 'package:_fe_analyzer_shared/src/macros/executor.dart';
import 'package:_fe_analyzer_shared/src/macros/executor/builder_impls.dart';
import 'package:_fe_analyzer_shared/src/macros/executor/exception_impls.dart';
import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart';

/// Runs [macro] in the types phase and returns a [MacroExecutionResult].
Expand Down Expand Up @@ -58,9 +59,15 @@ Future<MacroExecutionResult> executeTypesMacro(
'macro: $macro\ntarget: $target');
}
} catch (e, s) {
builder.report(new Diagnostic(
new DiagnosticMessage('Unhandled error: $e\n' 'Stack trace:\n$s'),
Severity.error));
// Preserve `MacroException`s thrown by SDK tools.
if (e is MacroExceptionImpl) {
builder.failWithException(e);
} else {
// Convert exceptions thrown by macro implementations into diagnostics.
builder.report(new Diagnostic(
new DiagnosticMessage('Unhandled error: $e\n' 'Stack trace:\n$s'),
Severity.error));
}
}
return builder.result;
}
Expand Down Expand Up @@ -130,9 +137,15 @@ Future<MacroExecutionResult> executeDeclarationsMacro(Macro macro,
'macro: $macro\ntarget: $target');
}
} catch (e, s) {
builder.report(new Diagnostic(
new DiagnosticMessage('Unhandled error: $e\n' 'Stack trace:\n$s'),
Severity.error));
// Preserve `MacroException`s thrown by SDK tools.
if (e is MacroExceptionImpl) {
builder.failWithException(e);
} else {
// Convert exceptions thrown by macro implementations into diagnostics.
builder.report(new Diagnostic(
new DiagnosticMessage('Unhandled error: $e\n' 'Stack trace:\n$s'),
Severity.error));
}
}
return builder.result;
}
Expand Down Expand Up @@ -199,9 +212,15 @@ Future<MacroExecutionResult> executeDefinitionMacro(Macro macro, Object target,
'macro: $macro\ntarget: $target');
}
} catch (e, s) {
builder.report(new Diagnostic(
new DiagnosticMessage('Unhandled error: $e\n' 'Stack trace:\n$s'),
Severity.error));
// Preserve `MacroException`s thrown by SDK tools.
if (e is MacroExceptionImpl) {
builder.failWithException(e);
} else {
// Convert exceptions thrown by macro implementations into diagnostics.
builder.report(new Diagnostic(
new DiagnosticMessage('Unhandled error: $e\n' 'Stack trace:\n$s'),
Severity.error));
}
}
return builder.result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import '../api.dart';
import '../executor.dart';
import 'exception_impls.dart';
import 'introspection_impls.dart';
import 'serialization.dart';
import 'serialization_extensions.dart';
Expand Down Expand Up @@ -263,6 +264,9 @@ class MacroExecutionResultImpl implements MacroExecutionResult {
@override
final List<Diagnostic> diagnostics;

@override
final MacroExceptionImpl? exception;

@override
final Map<IdentifierImpl, List<DeclarationCode>> enumValueAugmentations;

Expand All @@ -283,6 +287,7 @@ class MacroExecutionResultImpl implements MacroExecutionResult {

MacroExecutionResultImpl({
required this.diagnostics,
this.exception,
required this.enumValueAugmentations,
required this.interfaceAugmentations,
required this.libraryAugmentations,
Expand All @@ -299,6 +304,10 @@ class MacroExecutionResultImpl implements MacroExecutionResult {
for (; deserializer.moveNext();) deserializer.expectDiagnostic(),
];

MacroExceptionImpl? exception = (deserializer..moveNext()).checkNull()
? null
: deserializer.expectRemoteInstance();

deserializer
..moveNext()
..expectList();
Expand Down Expand Up @@ -379,6 +388,7 @@ class MacroExecutionResultImpl implements MacroExecutionResult {

return new MacroExecutionResultImpl(
diagnostics: diagnostics,
exception: exception,
enumValueAugmentations: enumValueAugmentations,
interfaceAugmentations: interfaceAugmentations,
libraryAugmentations: libraryAugmentations,
Expand All @@ -396,6 +406,12 @@ class MacroExecutionResultImpl implements MacroExecutionResult {
}
serializer.endList();

if (exception == null) {
serializer.addNull();
} else {
exception!.serialize(serializer);
}

serializer.startList();
for (IdentifierImpl enuum in enumValueAugmentations.keys) {
enuum.serialize(serializer);
Expand Down
9 changes: 8 additions & 1 deletion pkg/analyzer/lib/src/summary2/macro_application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,14 @@ class LibraryMacroApplier {
return true;
}

for (final diagnostic in result.diagnostics) {
for (final diagnostic in [
...result.diagnostics,
// TODO(scheglov): refactor to handle exceptions that are preserved
// through serialization directly instead of by parsing messages.
if (result.exception != null)
macro.Diagnostic(macro.DiagnosticMessage(result.exception.toString()),
macro.Severity.error),
]) {
if (addAsDeclarationsPhaseIntrospectionCycle(diagnostic)) {
continue;
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/front_end/lib/src/fasta/kernel/resource_identifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ import '../messages.dart'
messageResourceIdentifiersMultiple;
import 'constant_evaluator.dart' show ErrorReporter;

/// Get all annotations for the given [node] of the form
/// `@ResourceIdentifier(...)`
/// Get all of the `@ResourceIdentifier` annotations from `package:meta`
/// that are attached to the specified [node].
Iterable<InstanceConstant> findResourceAnnotations(Annotatable node) =>
node.annotations
.whereType<ConstantExpression>()
.map((expression) => expression.constant)
.whereType<InstanceConstant>()
.where((instance) => isResourceIdentifier(instance.classNode));
.where((instance) => isResourceIdentifier(instance.classNode))
.toList(growable: false);

final Uri _metaLibraryUri = new Uri(scheme: 'package', path: 'meta/meta.dart');

bool isResourceIdentifier(Class classNode) =>
classNode.name == 'ResourceIdentifier';
classNode.name == 'ResourceIdentifier' &&
classNode.enclosingLibrary.importUri == _metaLibraryUri;

/// Report if the resource annotations is placed on anything but a static
/// method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ class _MacroExecutionResult implements MacroExecutionResult {
@override
List<Diagnostic> diagnostics = [];

@override
MacroException? exception;

@override
Map<Identifier, Iterable<DeclarationCode>> enumValueAugmentations = const {};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024, 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.

void main() {
print(SomeClass.someStaticMethod(42));
}

class SomeClass {
@ResourceIdentifier('id')
static someStaticMethod(int i) {
return i + 1;
}
}

class ResourceIdentifier {
final Object? metadata;

const ResourceIdentifier([this.metadata]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library #lib;
import self as self;
import "dart:core" as core;

abstract class SomeClass extends core::Object {
[@vm.unboxing-info.metadata=()->i] static method someStaticMethod() → dynamic {
return [@vm.direct-call.metadata=dart.core::_IntegerImplementation.+] [@vm.inferred-type.metadata=int (skip check)] #C1.{core::num::+}(1){(core::num) → core::int};
}
}
static method main() → void {
core::print([@vm.inferred-type.metadata=int] self::SomeClass::someStaticMethod());
}
constants {
#C1 = 42
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"_comment": "Resources referenced by annotated resource identifiers",
"AppTag": "TBD",
"environment": {
"dart.tool.dart2js": false
},
"identifiers": []
}
2 changes: 1 addition & 1 deletion tools/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ CHANNEL dev
MAJOR 3
MINOR 4
PATCH 0
PRERELEASE 113
PRERELEASE 114
PRERELEASE_PATCH 0

0 comments on commit 29265c9

Please sign in to comment.