Skip to content

Frontend: Downgrade 'already enabled' to a warning for experimental features #77359

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
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
15 changes: 9 additions & 6 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,16 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.EnableExperimentalStringProcessing = true;
}

auto enableUpcomingFeature = [&Opts, &Diags](Feature feature) -> bool {
auto enableUpcomingFeature = [&Opts, &Diags](Feature feature,
bool downgradeDiag) -> bool {
// Check if this feature was introduced already in this language version.
if (auto firstVersion = getFeatureLanguageVersion(feature)) {
if (Opts.isSwiftVersionAtLeast(*firstVersion)) {
Diags.diagnose(SourceLoc(), diag::error_upcoming_feature_on_by_default,
getFeatureName(feature), *firstVersion);
return true;
Diags
.diagnose(SourceLoc(), diag::error_upcoming_feature_on_by_default,
getFeatureName(feature), *firstVersion)
.limitBehaviorIf(downgradeDiag, DiagnosticBehavior::Warning);
return !downgradeDiag;
}
}

Expand Down Expand Up @@ -1062,7 +1065,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
// -enable-experimental-feature flag too since the feature may have
// graduated from being experimental.
if (auto feature = getUpcomingFeature(value)) {
if (enableUpcomingFeature(*feature))
if (enableUpcomingFeature(*feature, /*downgradeDiag=*/true))
HadError = true;
}

Expand All @@ -1087,7 +1090,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
if (!feature)
continue;

if (enableUpcomingFeature(*feature))
if (enableUpcomingFeature(*feature, /*downgradeDiag=*/false))
HadError = true;
}

Expand Down
21 changes: 11 additions & 10 deletions test/Frontend/upcoming_feature.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
// Make sure that hasFeature(ConciseMagicFile) evaluates true when provided
// explicitly.
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile %s
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature ConciseMagicFile

// Make sure that hasFeature(ConciseMagicFile) evaluates true in Swift 6.
// RUN: %target-swift-frontend -typecheck -swift-version 6 %s
// RUN: %target-typecheck-verify-swift -swift-version 6

// Make sure that hasFeature(ConciseMagicFile) is off prior to Swift 6
// RUN: %target-typecheck-verify-swift %s
// RUN: %target-typecheck-verify-swift -verify-additional-prefix swift5-

// It's fine to provide a feature that we don't know about
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile -enable-upcoming-feature UnknownFeature %s
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature UnknownFeature -enable-upcoming-feature ConciseMagicFile %s
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature ConciseMagicFile -enable-upcoming-feature UnknownFeature
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature UnknownFeature -enable-upcoming-feature ConciseMagicFile

// For compatibility when a feature graduates, it's fine to refer to an
// upcoming feature as an experimental feature.
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature ConciseMagicFile %s
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ConciseMagicFile

// It's not fine to provide a feature that's in the specified language version.
// RUN: not %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s
// RUN: not %target-swift-frontend -typecheck -enable-experimental-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s
// RUN: not %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s --check-prefix=CHECK-ERROR
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s --check-prefix=CHECK-WARN

// CHECK: error: upcoming feature 'ConciseMagicFile' is already enabled as of Swift version 6
// CHECK-ERROR: error: upcoming feature 'ConciseMagicFile' is already enabled as of Swift version 6
// CHECK-WARN: warning: upcoming feature 'ConciseMagicFile' is already enabled as of Swift version 6

#if hasFeature(ConciseMagicFile)
let x = 0
#else
let y = boom // expected-error{{'boom'}}
let y = boom // expected-swift5-error{{'boom'}}
#endif