Skip to content

Commit 98a3605

Browse files
nshahanCommit Queue
authored and
Commit Queue
committed
[ddc] Fix assert location in expression eval
When assertions appear in a debugger expression they now have a synthetic source location available. This also allows for the lookup of the actual source. Issue: #43986 Fixes: #54956 Change-Id: I34ae5f810593b40282929d02cb290de86603922f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356287 Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Nicholas Shahan <[email protected]>
1 parent 97369d0 commit 98a3605

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,23 +4075,23 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
40754075

40764076
SourceLocation? location;
40774077
late String conditionSource;
4078-
if (node.location != null) {
4079-
var encodedSource =
4080-
node.enclosingComponent!.uriToSource[node.location!.file]!.source;
4078+
var assertLocation = node.location;
4079+
if (assertLocation != null) {
4080+
var fileUri = assertLocation.file;
4081+
var encodedSource = node.enclosingComponent!.uriToSource[fileUri]!.source;
40814082
var source = utf8.decode(encodedSource, allowMalformed: true);
4082-
40834083
conditionSource =
40844084
source.substring(node.conditionStartOffset, node.conditionEndOffset);
4085+
// Assertions that appear in debugger expressions have a synthetic Uri
4086+
// that is different than the current library where the expression will
4087+
// be evaluated.
4088+
var savedUri = _currentUri;
4089+
_currentUri = fileUri;
40854090
location = _toSourceLocation(node.conditionStartOffset)!;
4091+
_currentUri = savedUri;
40864092
} else {
4087-
// Location is null in expression compilation when modules
4088-
// are loaded from kernel using expression compiler worker.
4089-
// Show the error only in that case, with the condition AST
4090-
// instead of the source.
4091-
//
4092-
// TODO(annagrin): Can we add some information to the kernel,
4093-
// or add better printing for the condition?
4094-
// Issue: https://github.com/dart-lang/sdk/issues/43986
4093+
// If the location is ever null, only show the error with the condition
4094+
// AST instead of the source.
40954095
conditionSource = node.condition.toString();
40964096
}
40974097
return js.statement(' if (!#) #;', [

pkg/dev_compiler/test/expression_compiler/assertions_enabled_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ void main(List<String> args) async {
5858
expression: '() { assert(false); return 0; } ()',
5959
expectedError: allOf(
6060
contains('Error: Assertion failed:'),
61-
contains('<unknown source>:-1:-1'),
62-
contains('BoolLiteral(false)'),
61+
contains('org-dartlang-debug:synthetic_debug_expression:1:13'),
62+
contains('false'),
6363
contains('is not true'),
6464
));
6565
});

0 commit comments

Comments
 (0)