Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null exception in comment_writer #1621

Closed
fishythefish opened this issue Dec 12, 2024 · 3 comments · Fixed by #1623
Closed

Null exception in comment_writer #1621

fishythefish opened this issue Dec 12, 2024 · 3 comments · Fixed by #1623
Labels

Comments

@fishythefish
Copy link
Member

Hit a bug in the formatter when formatting pkg/compiler/test/closure/data/captured_variable.dart.
Please report at github.com/dart-lang/dart_style/issues.
Null check operator used on a null value
#0      CommentWriter._commentsBefore (package:dart_style/src/front_end/comment_writer.dart:81:47)
#1      CommentWriter.takeCommentsBefore (package:dart_style/src/front_end/comment_writer.dart:62:12)
#2      SequenceBuilder.addCommentsBefore (package:dart_style/src/front_end/sequence_builder.dart:125:38)
#3      SequenceBuilder.visit (package:dart_style/src/front_end/sequence_builder.dart:103:5)
#4      PieceFactory.writeBody (package:dart_style/src/front_end/piece_factory.dart:152:16)
#5      PieceFactory.writeBlock (package:dart_style/src/front_end/piece_factory.dart:172:5)
#6      AstNodeVisitor.visitBlock (package:dart_style/src/front_end/ast_node_visitor.dart:225:5)
#7      BlockImpl.accept (package:analyzer/src/dart/ast/ast.dart:2006:50)
#8      AstNodeVisitor.visitNode (package:dart_style/src/front_end/ast_node_visitor.dart:1969:10)
#9      PieceWriter.visit (package:dart_style/src/front_end/piece_writer.dart:145:14)
#10     AstNodeVisitor.visitBlockFunctionBody (package:dart_style/src/front_end/ast_node_visitor.dart:232:12)
#11     BlockFunctionBodyImpl.accept (package:analyzer/src/dart/ast/ast.dart:1958:50)
#12     AstNodeVisitor.visitNode (package:dart_style/src/front_end/ast_node_visitor.dart:1969:10)
#13     PieceWriter.visit (package:dart_style/src/front_end/piece_writer.dart:145:14)
#14     PieceFactory.writeFunction.<anonymous closure>.<anonymous closure> (package:dart_style/src/front_end/piece_factory.dart:597:16)
#15     PieceFactory.writeFunctionAndReturnType (package:dart_style/src/front_end/piece_factory.dart:615:20)
#16     PieceFactory.writeFunction.<anonymous closure> (package:dart_style/src/front_end/piece_factory.dart:584:7)
#17     PieceWriter.withMetadata (package:dart_style/src/front_end/piece_writer.dart:265:20)
#18     PieceFactory.writeFunction (package:dart_style/src/front_end/piece_factory.dart:583:12)
#19     AstNodeVisitor.visitFunctionDeclaration (package:dart_style/src/front_end/ast_node_visitor.dart:798:5)
#20     FunctionDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:8357:50)
#21     AstNodeVisitor.visitNode (package:dart_style/src/front_end/ast_node_visitor.dart:1969:10)
#22     PieceFactory.nodePiece.<anonymous closure> (package:dart_style/src/front_end/piece_factory.dart:1485:7)
#23     PieceWriter.build (package:dart_style/src/front_end/piece_writer.dart:189:18)
#24     PieceFactory.nodePiece (package:dart_style/src/front_end/piece_factory.dart:1484:25)
#25     SequenceBuilder.visit (package:dart_style/src/front_end/sequence_builder.dart:104:18)
#26     AstNodeVisitor.run (package:dart_style/src/front_end/ast_node_visitor.dart:112:18)
#27     DartFormatter.formatSource (package:dart_style/src/dart_formatter.dart:219:24)
#28     _processFile (package:dart_style/src/io.dart:175:28)
<asynchronous suspension>
#29     _processDirectory (package:dart_style/src/io.dart:134:10)
<asynchronous suspension>
#30     formatPaths (package:dart_style/src/io.dart:95:12)
<asynchronous suspension>
#31     FormatCommand.run (package:dart_style/src/cli/format_command.dart:267:7)
<asynchronous suspension>
#32     CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#33     DartdevRunner.runCommand (package:dartdev/dartdev.dart:240:18)
<asynchronous suspension>
#34     runDartdev (package:dartdev/dartdev.dart:47:16)
<asynchronous suspension>
#35     main (file:///b/f/w/pkg/dartdev/bin/dartdev.dart:13:5)
<asynchronous suspension>
@fishythefish
Copy link
Member Author

I suspect that this comes from having a snippet like this:

@override
// ignore: overridden_fields
final FunctionEntity _member;

(Note that this is how VSCode quick fix will add the ignore comment.)

If I change it to

@override
final FunctionEntity _member; // ignore: overridden_fields

then the formatter works fine.

@munificent munificent added the bug label Dec 12, 2024
@munificent
Copy link
Member

Thanks, I'll take a look.

@munificent
Copy link
Member

There may have been two issues in that file. With the latest dart_style, I didn't get a crash on the // ignore comment after a metadata annotation. If that's what was crashing for you, it was probably already fixed by #1612 (which I haven't rolled into the SDK yet).

But I did get another crash in that same file with the same stack trace. That one was caused by:

  var /**/ local = parameter;

Because Dart used to use Javadoc style doc comments ages ago, it seems the parser treats /**/ as a doc comment. Also, analyzer has a somewhat confusing thing where it treats doc comments part of the AST when the formatter really wants to handle them separately. Even more confusing, it treats that /**/ as a doc comment for the entire variable declaration statement even though it's embedded in the middle of it.

Either way, the formatter handles this (incredibly rare) case better now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants