Skip to content

Commit b9c0f5c

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
[CFE] Issue error when .packages specifies wrong language version
From the feature specification: "If an entry contains an invalid dart version value, #dart=arglebargle or #dart= or any other value which is not a semantic versioning version, then tools which need the version should report an error." Change-Id: Ieb54dbbad6f2fecb341e27b468e3ffdf90717a7c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112245 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 21b026d commit b9c0f5c

File tree

17 files changed

+137
-13
lines changed

17 files changed

+137
-13
lines changed

pkg/front_end/lib/src/fasta/fasta_codes_generated.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6570,6 +6570,16 @@ Message _withArgumentsLabelNotFound(String name) {
65706570
arguments: {'name': name});
65716571
}
65726572

6573+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
6574+
const Code<Null> codeLanguageVersionInvalidInDotPackages =
6575+
messageLanguageVersionInvalidInDotPackages;
6576+
6577+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
6578+
const MessageCode messageLanguageVersionInvalidInDotPackages = const MessageCode(
6579+
"LanguageVersionInvalidInDotPackages",
6580+
message:
6581+
r"""The language version is not specified correctly in the .packages file.""");
6582+
65736583
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
65746584
const Template<
65756585
Message Function(

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ class KernelTarget extends TargetImplementation {
173173
String asString = "$entryPoint";
174174
packagesMap ??= uriTranslator.packages.asMap();
175175
for (String packageName in packagesMap.keys) {
176-
String prefix = "${packagesMap[packageName]}";
176+
Uri packageUri = packagesMap[packageName];
177+
if (packageUri?.hasFragment == true) {
178+
packageUri = packageUri.removeFragment();
179+
}
180+
String prefix = "${packageUri}";
177181
if (asString.startsWith(prefix)) {
178182
Uri reversed = Uri.parse(
179183
"package:$packageName/${asString.substring(prefix.length)}");

pkg/front_end/lib/src/fasta/loader.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,15 @@ abstract class Loader {
121121
break;
122122
}
123123
}
124-
bool hasValidPackageSpecifiedLanguageVersion = false;
124+
bool hasPackageSpecifiedLanguageVersion = false;
125125
int packageSpecifiedLanguageVersionMajor;
126126
int packageSpecifiedLanguageVersionMinor;
127127
if (packageFragment != null) {
128128
List<String> properties = packageFragment.split("&");
129129
for (int i = 0; i < properties.length; ++i) {
130130
String property = properties[i];
131131
if (property.startsWith("dart=")) {
132+
hasPackageSpecifiedLanguageVersion = true;
132133
String langaugeVersionString = property.substring(5);
133134

134135
// Verify that the version is x.y[whatever]
@@ -138,23 +139,14 @@ abstract class Loader {
138139
int.tryParse(dotSeparatedParts[0]);
139140
packageSpecifiedLanguageVersionMinor =
140141
int.tryParse(dotSeparatedParts[1]);
141-
if (packageSpecifiedLanguageVersionMajor != null &&
142-
packageSpecifiedLanguageVersionMinor != null) {
143-
hasValidPackageSpecifiedLanguageVersion = true;
144-
}
145142
}
146-
147-
if (!hasValidPackageSpecifiedLanguageVersion) {
148-
// TODO(jensj): Issue error here.
149-
}
150-
151143
break;
152144
}
153145
}
154146
}
155147
LibraryBuilder library =
156148
target.createLibraryBuilder(uri, fileUri, origin);
157-
if (hasValidPackageSpecifiedLanguageVersion) {
149+
if (hasPackageSpecifiedLanguageVersion) {
158150
library.setLanguageVersion(packageSpecifiedLanguageVersionMajor,
159151
packageSpecifiedLanguageVersionMinor,
160152
explicit: false);

pkg/front_end/lib/src/fasta/source/source_library_builder.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import '../fasta_codes.dart'
9797
messageGenericFunctionTypeInBound,
9898
messageGenericFunctionTypeUsedAsActualTypeArgument,
9999
messageIncorrectTypeArgumentVariable,
100+
messageLanguageVersionInvalidInDotPackages,
100101
messagePartExport,
101102
messagePartExportContext,
102103
messagePartInPart,
@@ -365,6 +366,12 @@ class SourceLibraryBuilder extends LibraryBuilder {
365366
if (languageVersionExplicitlySet) return;
366367
if (explicit) languageVersionExplicitlySet = true;
367368

369+
if (major == null || minor == null) {
370+
addPostponedProblem(
371+
messageLanguageVersionInvalidInDotPackages, offset, length, fileUri);
372+
return;
373+
}
374+
368375
// If no language version has been set, the default is used.
369376
// If trying to set a langauge version that is higher than the "already-set"
370377
// version it's an error.

pkg/front_end/messages.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ InvalidVoid/part_wrapped_script1: Fail
334334
InvalidVoid/part_wrapped_script2: Fail
335335
InvalidVoid/script1: Fail
336336
InvalidVoid/script2: Fail
337+
LanguageVersionInvalidInDotPackages/analyzerCode: Fail
338+
LanguageVersionInvalidInDotPackages/part_wrapped_script: Fail # Importing file in the (now) part.
337339
LanguageVersionTooHigh/analyzerCode: Fail
338340
LanguageVersionTooHigh/part_wrapped_script: Fail # Content comes after "part of [...]" meaning it's not actually a language version specification.
339341
LibraryDirectiveNotFirst/part_wrapped_script1: Fail # Defining library name in the (now) part.

pkg/front_end/messages.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3617,4 +3617,11 @@ CombinedMemberSignatureFailed:
36173617
LanguageVersionTooHigh:
36183618
template: "The specified language version is too high. The highest supported language version is #count.#count2."
36193619
script: >
3620-
// @dart = 100.200
3620+
// @dart = 100.200
3621+
3622+
LanguageVersionInvalidInDotPackages:
3623+
template: "The language version is not specified correctly in the .packages file."
3624+
script:
3625+
main.dart: "import 'package:foo/foo.dart';"
3626+
lib/foo.dart: "// blah blah blah"
3627+
.packages: "foo:lib/#dart=arglebargle"

pkg/front_end/test/fasta/messages_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ class Compile extends Step<Example, Null, MessageTestSuite> {
510510
Uri output =
511511
suite.fileSystem.currentDirectory.resolve("$dir/main.dart.dill");
512512

513+
// Setup .packages if it doesn't exist.
514+
Uri dotPackagesUri =
515+
suite.fileSystem.currentDirectory.resolve("$dir/.packages");
516+
if (!await suite.fileSystem.entityForUri(dotPackagesUri).exists()) {
517+
suite.fileSystem.entityForUri(dotPackagesUri).writeAsBytesSync([]);
518+
}
519+
513520
print("Compiling $main");
514521
List<DiagnosticMessage> messages = <DiagnosticMessage>[];
515522

@@ -519,6 +526,7 @@ class Compile extends Step<Example, Null, MessageTestSuite> {
519526
.resolve("vm_platform_strong.dill")
520527
..target = new VmTarget(new TargetFlags())
521528
..fileSystem = new HybridFileSystem(suite.fileSystem)
529+
..packagesFileUri = dotPackagesUri
522530
..onDiagnostic = messages.add
523531
..environmentDefines = const {},
524532
main,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo:lib/#dart=arglebargle
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*error: LanguageVersionInvalidInDotPackages*/
2+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
3+
// for details. All rights reserved. Use of this source code is governed by a
4+
// BSD-style license that can be found in the LICENSE file.
5+
6+
/*library: languageVersion=2.8*/
7+
8+
import 'foo2.dart';
9+
10+
foo() {
11+
print("Hello from foo!");
12+
foo2();
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*error: LanguageVersionInvalidInDotPackages*/
2+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
3+
// for details. All rights reserved. Use of this source code is governed by a
4+
// BSD-style license that can be found in the LICENSE file.
5+
6+
// @dart = 2.4
7+
8+
/*library: languageVersion=2.4*/
9+
10+
foo2() {
11+
print("Hello from foo2!");
12+
}

0 commit comments

Comments
 (0)