-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macros] Add
DiagnosticException
that macro implementations can thr…
…ow to report a diagnostic. [email protected] Change-Id: I7546aa84f3e0b8423465dcb49d90a89df9231b84 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350302 Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Morgan :) <[email protected]> Reviewed-by: Jake Macdonald <[email protected]> Commit-Queue: Morgan :) <[email protected]>
- Loading branch information
1 parent
f4b1d59
commit f367d1e
Showing
4 changed files
with
55 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/language/macros/error/diagnostic_exception_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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. | ||
// SharedOptions=--enable-experiment=macros | ||
|
||
import 'impl/throw_diagnostic_exception_macro.dart'; | ||
|
||
@ThrowDiagnosticException(atTypeDeclaration: 'B', withMessage: 'failed here') | ||
class A {} | ||
|
||
class B {} | ||
// ^ | ||
// [analyzer] COMPILE_TIME_ERROR.MACRO_ERROR | ||
// [cfe] failed here |
21 changes: 21 additions & 0 deletions
21
tests/language/macros/error/impl/throw_diagnostic_exception_macro.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
// ignore_for_file: deprecated_member_use | ||
import 'package:_fe_analyzer_shared/src/macros/api.dart'; | ||
|
||
macro class ThrowDiagnosticException implements ClassDeclarationsMacro { | ||
final String atTypeDeclaration; | ||
final String withMessage; | ||
|
||
const ThrowDiagnosticException({ | ||
required this.atTypeDeclaration, required this.withMessage}); | ||
|
||
Future<void> buildDeclarationsForClass( | ||
ClassDeclaration clazz, MemberDeclarationBuilder builder) async { | ||
final identifier = await builder.resolveIdentifier(clazz.library.uri, atTypeDeclaration); | ||
final declaration = await builder.typeDeclarationOf(identifier); | ||
throw DiagnosticException(Diagnostic(DiagnosticMessage( | ||
withMessage, target: declaration.asDiagnosticTarget), Severity.error)); | ||
} | ||
} |