Skip to content

Commit

Permalink
[cfe] Disallow script tag in part file
Browse files Browse the repository at this point in the history
In response to #52575

Change-Id: I452575517c378dda3000b8fcbf4f66274b1583a6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315420
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
johnniwinther authored and Commit Queue committed Jul 21, 2023
1 parent 463e251 commit 46086e9
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11845,6 +11845,16 @@ const MessageCode messageReturnWithoutExpressionSync = const MessageCode(
problemMessage:
r"""A value must be explicitly returned from a non-void function.""");

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeScriptTagInPartFile = messageScriptTagInPartFile;

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const MessageCode messageScriptTagInPartFile = const MessageCode(
"ScriptTagInPartFile",
problemMessage: r"""A part file cannot have script tag.""",
correctionMessage:
r"""Try removing the script tag or the 'part of' directive.""");

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Template<Message Function(Uri uri_)> templateSdkRootNotFound =
const Template<Message Function(Uri uri_)>(
Expand Down
1 change: 1 addition & 0 deletions pkg/front_end/lib/src/fasta/source/outline_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,7 @@ class OutlineBuilder extends StackListenerImpl {
@override
void handleScript(Token token) {
debugEvent("Script");
libraryBuilder.addScriptToken(token.charOffset);
}

@override
Expand Down
11 changes: 11 additions & 0 deletions pkg/front_end/lib/src/fasta/source/source_library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {

Uri? partOfUri;

/// Offset of the first script tag (`#!...`) in this library or part.
int? _scriptTokenOffset;

List<MetadataBuilder>? metadata;

/// The current declaration that is being built. When we start parsing a
Expand Down Expand Up @@ -855,6 +858,14 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {
Uri newFileUri = resolve(fileUri, uri, uriOffset);
loader.read(partOfUri!, uriOffset, fileUri: newFileUri, accessor: this);
}
if (_scriptTokenOffset != null) {
addProblem(
messageScriptTagInPartFile, _scriptTokenOffset!, noLength, fileUri);
}
}

void addScriptToken(int charOffset) {
_scriptTokenOffset ??= charOffset;
}

void addFields(List<MetadataBuilder>? metadata, int modifiers,
Expand Down
2 changes: 2 additions & 0 deletions pkg/front_end/messages.status
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,8 @@ ReturnTypeFunctionExpression/example: Fail
ReturnWithoutExpressionAsync/analyzerCode: Fail
ReturnWithoutExpressionSync/analyzerCode: Fail
ReturnWithoutExpressionSync/part_wrapped_script: Fail
ScriptTagInPartFile/analyzerCode: Fail
ScriptTagInPartFile/example: Fail
SdkRootNotFound/analyzerCode: Fail
SdkRootNotFound/example: Fail
SdkSpecificationNotFound/analyzerCode: Fail
Expand Down
6 changes: 5 additions & 1 deletion pkg/front_end/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7028,4 +7028,8 @@ JointPatternVariableWithLabelDefault:
test(x) { switch(x) { case var a: default: return a; } }
UnsupportedMacroApplication:
problemMessage: "This macro application didn't apply correctly."
problemMessage: "This macro application didn't apply correctly."

ScriptTagInPartFile:
problemMessage: "A part file cannot have script tag."
correctionMessage: "Try removing the script tag or the 'part of' directive."
7 changes: 7 additions & 0 deletions pkg/front_end/testcases/general/script_tag_in_part_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env dart
// Copyright (c) 2023, 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.

part of 'script_tag_in_part_file_lib.dart';
void run() => print("Running!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/script_tag_in_part_file.dart:1:1: Error: A part file cannot have script tag.
// Try removing the script tag or the 'part of' directive.
// #!/usr/bin/env dart
// ^
//
import self as self;
import "dart:core" as core;

part script_tag_in_part_file.dart;
static method /* from org-dartlang-testcase:///script_tag_in_part_file.dart */ run() → void
return core::print("Running!");
static method main() → void
return self::run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/script_tag_in_part_file.dart:1:1: Error: A part file cannot have script tag.
// Try removing the script tag or the 'part of' directive.
// #!/usr/bin/env dart
// ^
//
import self as self;
import "dart:core" as core;

part script_tag_in_part_file.dart;
static method /* from org-dartlang-testcase:///script_tag_in_part_file.dart */ run() → void
return core::print("Running!");
static method main() → void
return self::run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env dart
part of 'script_tag_in_part_file_lib.dart';
void run() => print("Running!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/script_tag_in_part_file.dart:1:1: Error: A part file cannot have script tag.
// Try removing the script tag or the 'part of' directive.
// #!/usr/bin/env dart
// ^
//
import self as self;
import "dart:core" as core;

part script_tag_in_part_file.dart;
static method /* from org-dartlang-testcase:///script_tag_in_part_file.dart */ run() → void
return core::print("Running!");
static method main() → void
return self::run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/script_tag_in_part_file.dart:1:1: Error: A part file cannot have script tag.
// Try removing the script tag or the 'part of' directive.
// #!/usr/bin/env dart
// ^
//
import self as self;
import "dart:core" as core;

part script_tag_in_part_file.dart;
static method /* from org-dartlang-testcase:///script_tag_in_part_file.dart */ run() → void
return core::print("Running!");
static method main() → void
return self::run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/script_tag_in_part_file.dart:1:1: Error: A part file cannot have script tag.
// Try removing the script tag or the 'part of' directive.
// #!/usr/bin/env dart
// ^
//
import self as self;

part script_tag_in_part_file.dart;
static method /* from org-dartlang-testcase:///script_tag_in_part_file.dart */ run() → void
;
static method main() → void
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/script_tag_in_part_file.dart:1:1: Error: A part file cannot have script tag.
// Try removing the script tag or the 'part of' directive.
// #!/usr/bin/env dart
// ^
//
import self as self;
import "dart:core" as core;

part script_tag_in_part_file.dart;
static method /* from org-dartlang-testcase:///script_tag_in_part_file.dart */ run() → void
return core::print("Running!");
static method main() → void
return self::run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env dart
// Copyright (c) 2023, 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.

part 'script_tag_in_part_file.dart';

void main() => run();
1 change: 1 addition & 0 deletions pkg/front_end/testcases/textual_outline.status
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ general/null_safety_invalid_experiment: FormatterCrash
general/null_safety_invalid_experiment_and_language_version: FormatterCrash
general/records_opt_out: FormatterCrash
general/sealed_class_declaration: FormatterCrash
general/script_tag_in_part_file: FormatterCrash
general/type_parameters_on_void: FormatterCrash
general/type_variable_in_static_context: FormatterCrash
general/var_as_type_name: FormatterCrash
Expand Down

0 comments on commit 46086e9

Please sign in to comment.