Skip to content

Commit aac8204

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
[CFE] Error when library and part(s) doesn't agree on language version
From the feature spec: "Part files must be marked with the same version as their library. They must always be treated the same way as the library they belong to, so it is a compile-time error if a part file has a different language level override than its library. It is also a compile-time error if a part file has no language version marker, and the importing library does". Change-Id: I29805e261e63b89a7d8832770ed2b007fb6df37f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112247 Commit-Queue: Jens Johansen <jensj@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
1 parent b9c0f5c commit aac8204

File tree

11 files changed

+108
-1
lines changed

11 files changed

+108
-1
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
@@ -6580,6 +6580,16 @@ const MessageCode messageLanguageVersionInvalidInDotPackages = const MessageCode
65806580
message:
65816581
r"""The language version is not specified correctly in the .packages file.""");
65826582

6583+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
6584+
const Code<Null> codeLanguageVersionMismatchInPart =
6585+
messageLanguageVersionMismatchInPart;
6586+
6587+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
6588+
const MessageCode messageLanguageVersionMismatchInPart = const MessageCode(
6589+
"LanguageVersionMismatchInPart",
6590+
message:
6591+
r"""The language version override has to be the same in the library and its part(s).""");
6592+
65836593
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
65846594
const Template<
65856595
Message Function(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import '../fasta_codes.dart'
9898
messageGenericFunctionTypeUsedAsActualTypeArgument,
9999
messageIncorrectTypeArgumentVariable,
100100
messageLanguageVersionInvalidInDotPackages,
101+
messageLanguageVersionMismatchInPart,
101102
messagePartExport,
102103
messagePartExportContext,
103104
messagePartInPart,
@@ -901,6 +902,21 @@ class SourceLibraryBuilder extends LibraryBuilder {
901902
}
902903
return false;
903904
}
905+
906+
// Language versions have to match.
907+
if ((this.languageVersionExplicitlySet !=
908+
part.languageVersionExplicitlySet) ||
909+
(this.library.languageVersionMajor !=
910+
part.library.languageVersionMajor ||
911+
this.library.languageVersionMinor !=
912+
part.library.languageVersionMinor)) {
913+
// This is an error, but the part is not removed from the list of
914+
// parts, so that metadata annotations can be associated with it.
915+
addProblem(
916+
messageLanguageVersionMismatchInPart, partOffset, noLength, fileUri);
917+
return false;
918+
}
919+
904920
part.validatePart(this, usedParts);
905921
NameIterator partDeclarations = part.nameIterator;
906922
while (partDeclarations.moveNext()) {

pkg/front_end/messages.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ InvalidVoid/script1: Fail
336336
InvalidVoid/script2: Fail
337337
LanguageVersionInvalidInDotPackages/analyzerCode: Fail
338338
LanguageVersionInvalidInDotPackages/part_wrapped_script: Fail # Importing file in the (now) part.
339+
LanguageVersionMismatchInPart/analyzerCode: Fail
340+
LanguageVersionMismatchInPart/part_wrapped_script: Fail # Part in (now) part.
339341
LanguageVersionTooHigh/analyzerCode: Fail
340342
LanguageVersionTooHigh/part_wrapped_script: Fail # Content comes after "part of [...]" meaning it's not actually a language version specification.
341343
LibraryDirectiveNotFirst/part_wrapped_script1: Fail # Defining library name in the (now) part.

pkg/front_end/messages.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3624,4 +3624,16 @@ LanguageVersionInvalidInDotPackages:
36243624
script:
36253625
main.dart: "import 'package:foo/foo.dart';"
36263626
lib/foo.dart: "// blah blah blah"
3627-
.packages: "foo:lib/#dart=arglebargle"
3627+
.packages: "foo:lib/#dart=arglebargle"
3628+
3629+
LanguageVersionMismatchInPart:
3630+
template: "The language version override has to be the same in the library and its part(s)."
3631+
script:
3632+
main.dart: >
3633+
// @dart = 2.4
3634+
3635+
part 'part.dart';
3636+
part.dart: >
3637+
// @dart = 2.3
3638+
3639+
part of 'main.dart';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// @dart = 2.5
6+
7+
part /*error: LanguageVersionMismatchInPart*/ 'part.dart';
8+
9+
/*library: languageVersion=2.5*/
10+
11+
main() {
12+
/*error: MethodNotFound*/ foo();
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// @dart = 2.4
6+
7+
part of 'main.dart';
8+
9+
foo() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo:lib/#dart=2.5
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// The library and its part is both technically at language version 2.5,
6+
// but one is explicitly set, the other is not. That's an error.
7+
8+
// @dart = 2.5
9+
10+
part /*error: LanguageVersionMismatchInPart*/ 'part.dart';
11+
12+
/*library: languageVersion=2.5*/
13+
14+
foo() {
15+
/*error: MethodNotFound*/ bar();
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of 'foo.dart';
6+
7+
bar() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Set version of this file (not technically in package) explicitly to test as
6+
// much as possibly separately.
7+
8+
// @dart = 2.4
9+
10+
import 'package:foo/foo.dart';
11+
12+
/*library: languageVersion=2.4*/
13+
14+
main() {
15+
main();
16+
}

0 commit comments

Comments
 (0)