Skip to content

Commit 2b7fc8f

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Update FeatureSet to the new rules.
Bug: #43032 Change-Id: I6c7d7dc56fd0d4edcc3c6f6cc6fb0422316b8f4c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/159187 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 816fd08 commit 2b7fc8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+909
-244
lines changed

pkg/analysis_server/lib/src/analysis_server_abstract.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import 'package:analyzer/file_system/physical_file_system.dart';
3535
import 'package:analyzer/instrumentation/instrumentation.dart';
3636
import 'package:analyzer/src/dart/analysis/byte_store.dart';
3737
import 'package:analyzer/src/dart/analysis/driver.dart' as nd;
38+
import 'package:analyzer/src/dart/analysis/experiments.dart'
39+
as analyzer_features;
3840
import 'package:analyzer/src/dart/analysis/file_byte_store.dart'
3941
show EvictingFileByteStore;
4042
import 'package:analyzer/src/dart/analysis/file_state.dart' as nd;
@@ -157,8 +159,10 @@ abstract class AbstractAnalysisServer {
157159
var pluginWatcher = PluginWatcher(resourceProvider, pluginManager);
158160

159161
defaultContextOptions.contextFeatures =
160-
analyzer_features.FeatureSet.fromEnableFlags(
161-
options.enabledExperiments);
162+
analyzer_features.FeatureSet.fromEnableFlags2(
163+
sdkLanguageVersion: analyzer_features.ExperimentStatus.currentVersion,
164+
flags: options.enabledExperiments,
165+
);
162166
defaultContextOptions.useFastaParser = options.useFastaParser;
163167

164168
{

pkg/analyzer/lib/dart/analysis/features.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,26 @@ abstract class FeatureSet {
6464

6565
/// Computes the set of features implied by the given set of experimental
6666
/// enable flags.
67+
@Deprecated("Use 'fromEnableFlags2' instead")
6768
factory FeatureSet.fromEnableFlags(List<String> flags) =
6869
ExperimentStatus.fromStrings;
6970

71+
/// Computes the set of features implied by the given set of experimental
72+
/// enable flags.
73+
factory FeatureSet.fromEnableFlags2({
74+
@required Version sdkLanguageVersion,
75+
@required List<String> flags,
76+
}) = ExperimentStatus.fromStrings2;
77+
78+
/// Computes the set of features for the latest language version known
79+
/// to the analyzer, without any experiments. Use it only if you really
80+
/// don't care which language version you want to use, and sure that the
81+
/// code that you process is valid for the latest language version.
82+
///
83+
/// Otherwise, it is recommended to use [FeatureSet.fromEnableFlags2].
84+
factory FeatureSet.latestLanguageVersion() =
85+
ExperimentStatus.latestLanguageVersion;
86+
7087
/// Queries whether the given [feature] is contained in this feature set.
7188
bool isEnabled(Feature feature);
7289

pkg/analyzer/lib/dart/analysis/utilities.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ParseStringResult parseString(
7878
FeatureSet featureSet,
7979
String path,
8080
bool throwIfDiagnostics = true}) {
81-
featureSet ??= FeatureSet.fromEnableFlags([]);
81+
featureSet ??= FeatureSet.latestLanguageVersion();
8282
var source = StringSource(content, path);
8383
var reader = CharSequenceReader(content);
8484
var errorCollector = RecordingErrorListener();

pkg/analyzer/lib/dart/sdk/build_sdk_summary.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ class _Builder {
158158
if (pathSegments.isNotEmpty) {
159159
var libraryName = pathSegments.first;
160160
var experiments = allowedExperiments.forSdkLibrary(libraryName);
161-
return FeatureSet.fromEnableFlags(experiments);
161+
return FeatureSet.fromEnableFlags2(
162+
sdkLanguageVersion: languageVersion,
163+
flags: experiments,
164+
);
162165
}
163166
}
164167
throw StateError('Expected a valid dart: URI: $uri');

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ typedef WorkToWaitAfterComputingResult = Future<void> Function(String path);
8585
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
8686
class AnalysisDriver implements AnalysisDriverGeneric {
8787
/// The version of data format, should be incremented on every format change.
88-
static const int DATA_VERSION = 109;
88+
static const int DATA_VERSION = 110;
8989

9090
/// The length of the list returned by [_computeDeclaredVariablesSignature].
9191
static const int _declaredVariablesSignatureLength = 4;

pkg/analyzer/lib/src/dart/analysis/experiments.dart

Lines changed: 156 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,148 @@ export 'package:analyzer/src/dart/analysis/experiments_impl.dart'
2020

2121
part 'experiments.g.dart';
2222

23-
/// Gets access to the private list of boolean flags in an [Experiments] object.
24-
/// For testing use only.
23+
/// Gets access to the private list of boolean flags in an [ExperimentStatus]
24+
/// object. For testing use only.
2525
@visibleForTesting
2626
List<bool> getExperimentalFlags_forTesting(ExperimentStatus status) =>
27-
status._enableFlags;
27+
status._flags;
28+
29+
/// Gets access to the private SDK language version in an [ExperimentStatus]
30+
/// object. For testing use only.
31+
@visibleForTesting
32+
Version getSdkLanguageVersion_forTesting(ExperimentStatus status) =>
33+
status._sdkLanguageVersion;
2834

2935
/// A representation of the set of experiments that are active and whether they
3036
/// are enabled.
3137
class ExperimentStatus with _CurrentState implements FeatureSet {
3238
/// The current language version.
3339
static final Version currentVersion = Version.parse(_currentVersion);
3440

41+
/// The language version to use in tests.
42+
static final Version testingSdkLanguageVersion = Version.parse('2.10.0');
43+
44+
/// The latest known language version.
45+
static final Version latestSdkLanguageVersion = Version.parse('2.10.0');
46+
47+
static final FeatureSet latestWithNullSafety = ExperimentStatus.fromStrings2(
48+
sdkLanguageVersion: latestSdkLanguageVersion,
49+
flags: [EnableString.non_nullable],
50+
);
51+
3552
/// A map containing information about all known experimental flags.
3653
static final Map<String, ExperimentalFeature> knownFeatures = _knownFeatures;
3754

38-
final List<bool> _enableFlags;
55+
final Version _sdkLanguageVersion;
56+
final List<bool> _explicitEnabledFlags;
57+
final List<bool> _explicitDisabledFlags;
58+
final List<bool> _flags;
3959

40-
/// Initializes a newly created set of experiments based on optional
41-
/// arguments.
42-
ExperimentStatus() : _enableFlags = _buildExperimentalFlagsArray();
60+
factory ExperimentStatus() {
61+
return ExperimentStatus.latestLanguageVersion();
62+
}
4363

4464
/// Computes a set of features for use in a unit test. Computes the set of
4565
/// features enabled in [sdkVersion], plus any specified [additionalFeatures].
4666
///
4767
/// If [sdkVersion] is not supplied (or is `null`), then the current set of
4868
/// enabled features is used as the starting point.
4969
@visibleForTesting
50-
ExperimentStatus.forTesting(
51-
{String sdkVersion, List<Feature> additionalFeatures = const []})
52-
: this._(enableFlagsForTesting(
53-
sdkVersion: sdkVersion, additionalFeatures: additionalFeatures));
70+
factory ExperimentStatus.forTesting(
71+
// ignore:avoid_unused_constructor_parameters
72+
{String sdkVersion,
73+
List<Feature> additionalFeatures = const []}) {
74+
var explicitFlags = decodeExplicitFlags([]);
75+
for (ExperimentalFeature feature in additionalFeatures) {
76+
explicitFlags.enabled[feature.index] = true;
77+
}
78+
79+
var sdkLanguageVersion = latestSdkLanguageVersion;
80+
var flags = restrictEnableFlagsToVersion(
81+
sdkLanguageVersion: sdkLanguageVersion,
82+
explicitEnabledFlags: explicitFlags.enabled,
83+
explicitDisabledFlags: explicitFlags.disabled,
84+
version: sdkLanguageVersion,
85+
);
86+
87+
return ExperimentStatus._(
88+
sdkLanguageVersion,
89+
explicitFlags.enabled,
90+
explicitFlags.disabled,
91+
flags,
92+
);
93+
}
94+
95+
factory ExperimentStatus.fromStorage(List<int> encoded) {
96+
var allFlags = encoded.skip(2).map((e) => e != 0).toList();
97+
var featureCount = allFlags.length ~/ 3;
98+
return ExperimentStatus._(
99+
Version(encoded[0], encoded[1], 0),
100+
allFlags.sublist(0, featureCount),
101+
allFlags.sublist(featureCount, featureCount * 2),
102+
allFlags.sublist(featureCount * 2, featureCount * 3),
103+
);
104+
}
105+
106+
/// Decodes the strings given in [flags] into a representation of the set of
107+
/// experiments that should be enabled.
108+
///
109+
/// Always succeeds, even if the input flags are invalid. Expired and
110+
/// unrecognized flags are ignored, conflicting flags are resolved in favor of
111+
/// the flag appearing last.
112+
factory ExperimentStatus.fromStrings(List<String> flags) {
113+
return ExperimentStatus.fromStrings2(
114+
sdkLanguageVersion: latestSdkLanguageVersion,
115+
flags: flags,
116+
);
117+
}
54118

55119
/// Decodes the strings given in [flags] into a representation of the set of
56120
/// experiments that should be enabled.
57121
///
58122
/// Always succeeds, even if the input flags are invalid. Expired and
59123
/// unrecognized flags are ignored, conflicting flags are resolved in favor of
60124
/// the flag appearing last.
61-
ExperimentStatus.fromStrings(List<String> flags) : this._(decodeFlags(flags));
125+
factory ExperimentStatus.fromStrings2({
126+
@required Version sdkLanguageVersion,
127+
@required List<String> flags,
128+
// TODO(scheglov) use restrictEnableFlagsToVersion
129+
}) {
130+
var explicitFlags = decodeExplicitFlags(flags);
131+
132+
var decodedFlags = restrictEnableFlagsToVersion(
133+
sdkLanguageVersion: sdkLanguageVersion,
134+
explicitEnabledFlags: explicitFlags.enabled,
135+
explicitDisabledFlags: explicitFlags.disabled,
136+
version: sdkLanguageVersion,
137+
);
138+
139+
return ExperimentStatus._(
140+
sdkLanguageVersion,
141+
explicitFlags.enabled,
142+
explicitFlags.disabled,
143+
decodedFlags,
144+
);
145+
}
146+
147+
factory ExperimentStatus.latestLanguageVersion() {
148+
return ExperimentStatus.fromStrings2(
149+
sdkLanguageVersion: latestSdkLanguageVersion,
150+
flags: [],
151+
);
152+
}
62153

63-
ExperimentStatus._(this._enableFlags);
154+
ExperimentStatus._(
155+
this._sdkLanguageVersion,
156+
this._explicitEnabledFlags,
157+
this._explicitDisabledFlags,
158+
this._flags,
159+
);
64160

65161
@override
66162
int get hashCode {
67163
int hash = 0;
68-
for (var flag in _enableFlags) {
164+
for (var flag in _flags) {
69165
hash = JenkinsSmiHash.combine(hash, flag.hashCode);
70166
}
71167
return JenkinsSmiHash.finish(hash);
@@ -74,9 +170,19 @@ class ExperimentStatus with _CurrentState implements FeatureSet {
74170
@override
75171
bool operator ==(Object other) {
76172
if (other is ExperimentStatus) {
77-
if (_enableFlags.length != other._enableFlags.length) return false;
78-
for (int i = 0; i < _enableFlags.length; i++) {
79-
if (_enableFlags[i] != other._enableFlags[i]) return false;
173+
if (_sdkLanguageVersion != other._sdkLanguageVersion) {
174+
return false;
175+
}
176+
if (!_equalListOfBool(
177+
_explicitEnabledFlags, other._explicitEnabledFlags)) {
178+
return false;
179+
}
180+
if (!_equalListOfBool(
181+
_explicitDisabledFlags, other._explicitDisabledFlags)) {
182+
return false;
183+
}
184+
if (!_equalListOfBool(_flags, other._flags)) {
185+
return false;
80186
}
81187
return true;
82188
}
@@ -86,16 +192,42 @@ class ExperimentStatus with _CurrentState implements FeatureSet {
86192
/// Queries whether the given [feature] is enabled or disabled.
87193
@override
88194
bool isEnabled(covariant ExperimentalFeature feature) =>
89-
_enableFlags[feature.index];
195+
_flags[feature.index];
90196

91197
@override
92-
FeatureSet restrictToVersion(Version version) =>
93-
ExperimentStatus._(restrictEnableFlagsToVersion(_enableFlags, version));
198+
FeatureSet restrictToVersion(Version version) {
199+
return ExperimentStatus._(
200+
_sdkLanguageVersion,
201+
_explicitEnabledFlags,
202+
_explicitDisabledFlags,
203+
restrictEnableFlagsToVersion(
204+
sdkLanguageVersion: _sdkLanguageVersion,
205+
explicitEnabledFlags: _explicitEnabledFlags,
206+
explicitDisabledFlags: _explicitDisabledFlags,
207+
version: version,
208+
),
209+
);
210+
}
211+
212+
/// Encode into the format suitable for [ExperimentStatus.fromStorage].
213+
List<int> toStorage() {
214+
return [
215+
_sdkLanguageVersion.major,
216+
_sdkLanguageVersion.minor,
217+
..._explicitEnabledFlags.map((e) => e ? 1 : 0),
218+
..._explicitDisabledFlags.map((e) => e ? 1 : 0),
219+
..._flags.map((e) => e ? 1 : 0),
220+
];
221+
}
94222

95223
@override
96-
String toString() => experimentStatusToString(_enableFlags);
224+
String toString() => experimentStatusToString(_flags);
97225

98-
/// Returns a list of strings suitable for passing to
99-
/// [ExperimentStatus.fromStrings].
100-
List<String> toStringList() => experimentStatusToStringList(this);
226+
static bool _equalListOfBool(List<bool> first, List<bool> second) {
227+
if (first.length != second.length) return false;
228+
for (var i = 0; i < first.length; i++) {
229+
if (first[i] != second[i]) return false;
230+
}
231+
return true;
232+
}
101233
}

pkg/analyzer/lib/src/dart/analysis/experiments.g.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ final _knownFeatures = <String, ExperimentalFeature>{
2626
EnableString.variance: ExperimentalFeatures.variance,
2727
};
2828

29-
List<bool> _buildExperimentalFlagsArray() => <bool>[
30-
true, // constant-update-2018
31-
true, // control-flow-collections
32-
true, // extension-methods
33-
IsEnabledByDefault.non_nullable,
34-
IsEnabledByDefault.nonfunction_type_aliases,
35-
true, // set-literals
36-
true, // spread-collections
37-
IsEnabledByDefault.triple_shift,
38-
IsEnabledByDefault.value_class,
39-
IsEnabledByDefault.variance,
40-
];
41-
4229
/// Constant strings for enabling each of the currently known experimental
4330
/// flags.
4431
class EnableString {

0 commit comments

Comments
 (0)