Skip to content

Frontend: Really allow any experimental feature when compiling a module interface #72681

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,14 @@ namespace swift {
/// type-checking, SIL verification, and IR emission,
bool BypassResilienceChecks = false;

/// Whether or not to allow experimental features that are only available
/// in "production".
#ifdef NDEBUG
bool RestrictNonProductionExperimentalFeatures = true;
#else
bool RestrictNonProductionExperimentalFeatures = false;
#endif

bool isConcurrencyModelTaskToThread() const {
return ActiveConcurrencyModel == ConcurrencyModel::TaskToThread;
}
Expand Down
7 changes: 2 additions & 5 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,17 +872,14 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
// If this is a known experimental feature, allow it in +Asserts
// (non-release) builds for testing purposes.
if (auto feature = getExperimentalFeature(value)) {
#ifdef NDEBUG
if (!buildingFromInterface && !isFeatureAvailableInProduction(*feature)) {
if (Opts.RestrictNonProductionExperimentalFeatures &&
!isFeatureAvailableInProduction(*feature)) {
Diags.diagnose(SourceLoc(), diag::experimental_not_supported_in_production,
A->getValue());
HadError = true;
} else {
Opts.enableFeature(*feature);
}
#else
Opts.enableFeature(*feature);
#endif

if (*feature == Feature::NoncopyableGenerics2)
Opts.enableFeature(Feature::NoncopyableGenerics);
Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,12 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
bool StrictImplicitModuleContext =
subInvocation.getFrontendOptions().StrictImplicitModuleContext;

// It isn't appropriate to restrict use of experimental features in another
// module since it may have been built with a different compiler that allowed
// the use of the feature.
subInvocation.getLangOptions().RestrictNonProductionExperimentalFeatures =
false;

// Save the target triple from the original context.
llvm::Triple originalTargetTriple(subInvocation.getLangOptions().Target);

Expand Down
12 changes: 12 additions & 0 deletions test/Frontend/experimental-features-asserts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This test verifies that command line parsing allows use of any features with
// an asserts compilers.

// REQUIRES: asserts

// 'AccessLevelOnImport' is allowed in production
// RUN: %target-swift-frontend -typecheck %s -enable-experimental-feature AccessLevelOnImport -verify

// 'ParserValidation' is NOT allowed in production, but we are building with an asserts compiler.
// RUN: %target-swift-frontend -typecheck %s -enable-experimental-feature ParserValidation

import Swift
14 changes: 14 additions & 0 deletions test/Frontend/experimental-features-no-asserts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This test verifies that command line parsing restricts use of features that
// are not allowed non-production compilers.

// REQUIRES: no_asserts

// 'AccessLevelOnImport' is allowed in production
// RUN: %target-swift-frontend -typecheck %s -enable-experimental-feature AccessLevelOnImport -verify

// 'ParserValidation' is NOT allowed in production
// RUN: not %target-swift-frontend -typecheck %s -enable-experimental-feature ParserValidation 2>&1 | %FileCheck %s

// CHECK: <unknown>:0: error: experimental feature 'ParserValidation' cannot be enabled in production compiler

import Swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name ExperimentalFeatures -enable-experimental-feature ParserRoundTrip
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// Building a module from this interface should always succeed, even though
// ParserRoundTrip is an experimental feature that is not enabled in production
// compilers.

// RUN: %target-swift-frontend -compile-module-from-interface -module-name ExperimentalFeatures -o /dev/null %s -verify
// RUN: %target-swift-frontend -typecheck-module-from-interface -module-name ExperimentalFeatures %s -verify
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t
// RUN: %target-swift-frontend -compile-module-from-interface -module-name ExperimentalFeatures -o /dev/null %t/ExperimentalFeatures.swiftinterface -verify
// RUN: %target-swift-frontend -typecheck-module-from-interface -module-name ExperimentalFeatures %t/ExperimentalFeatures.swiftinterface -verify

//--- ExperimentalFeatures.swiftinterface
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name ExperimentalFeatures -enable-experimental-feature ParserRoundTrip

import Swift
extension Int {
public static var fortytwo: Int = 42
}

//--- Client.swift

import ExperimentalFeatures

_ = Int.fortytwo