Skip to content

Commit 013975a

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 9480a67 + 31408fe commit 013975a

File tree

16 files changed

+145
-45
lines changed

16 files changed

+145
-45
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,10 @@ namespace swift {
618618

619619
/// Figure out the Behavior for the given diagnostic, taking current
620620
/// state such as fatality into account.
621-
DiagnosticBehavior determineBehavior(const Diagnostic &diag);
621+
DiagnosticBehavior determineBehavior(const Diagnostic &diag) const;
622+
623+
/// Updates the diagnostic state for a diagnostic to emit.
624+
void updateFor(DiagnosticBehavior behavior);
622625

623626
bool hadAnyError() const { return anyErrorOccurred; }
624627
bool hasFatalErrorOccurred() const { return fatalErrorOccurred; }
@@ -646,7 +649,7 @@ namespace swift {
646649

647650
/// Returns a Boolean value indicating whether warnings belonging to the
648651
/// diagnostic group identified by `id` should be escalated to errors.
649-
bool getWarningsAsErrorsForDiagGroupID(DiagGroupID id) {
652+
bool getWarningsAsErrorsForDiagGroupID(DiagGroupID id) const {
650653
return warningsAsErrors[(unsigned)id];
651654
}
652655

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5299,15 +5299,15 @@ SourceLoc extractNearestSourceLoc(RegexLiteralPatternFeatureKind kind);
52995299

53005300
class GenericTypeParamDeclGetValueTypeRequest
53015301
: public SimpleRequest<GenericTypeParamDeclGetValueTypeRequest,
5302-
Type(GenericTypeParamDecl *decl),
5302+
Type(const GenericTypeParamDecl *decl),
53035303
RequestFlags::Cached> {
53045304
public:
53055305
using SimpleRequest::SimpleRequest;
53065306

53075307
private:
53085308
friend SimpleRequest;
53095309

5310-
Type evaluate(Evaluator &evaluator, GenericTypeParamDecl *decl) const;
5310+
Type evaluate(Evaluator &evaluator, const GenericTypeParamDecl *decl) const;
53115311

53125312
public:
53135313
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ SWIFT_REQUEST(TypeChecker, CustomDerivativesRequest,
626626
Cached, NoLocationInfo)
627627

628628
SWIFT_REQUEST(TypeChecker, GenericTypeParamDeclGetValueTypeRequest,
629-
Type(GenericTypeParamDecl *), Cached, NoLocationInfo)
629+
Type(const GenericTypeParamDecl *), Cached, NoLocationInfo)
630630

631631
SWIFT_REQUEST(TypeChecker, SemanticAvailableAttrRequest,
632632
std::optional<SemanticAvailableAttr>
@@ -643,4 +643,4 @@ SWIFT_REQUEST(TypeChecker, DefaultIsolationInSourceFileRequest,
643643

644644
SWIFT_REQUEST(TypeChecker, ModuleHasTypeCheckerPerformanceHacksEnabledRequest,
645645
bool(const ModuleDecl *),
646-
Cached, NoLocationInfo)
646+
Cached, NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6121,9 +6121,13 @@ GenericTypeParamDecl *GenericTypeParamDecl::createImplicit(
61216121
}
61226122

61236123
Type GenericTypeParamDecl::getValueType() const {
6124-
return evaluateOrDefault(getASTContext().evaluator,
6125-
GenericTypeParamDeclGetValueTypeRequest{const_cast<GenericTypeParamDecl *>(this)},
6126-
Type());
6124+
if (!isValue())
6125+
return Type();
6126+
6127+
auto &ctx = getASTContext();
6128+
GenericTypeParamDeclGetValueTypeRequest req(this);
6129+
auto ty = evaluateOrDefault(ctx.evaluator, req, Type());
6130+
return ty ? ty : ErrorType::get(ctx);
61276131
}
61286132

61296133
SourceRange GenericTypeParamDecl::getSourceRange() const {

lib/AST/DiagnosticEngine.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,8 @@ llvm::cl::opt<bool> AssertOnError("swift-diagnostics-assert-on-error",
12871287
llvm::cl::opt<bool> AssertOnWarning("swift-diagnostics-assert-on-warning",
12881288
llvm::cl::init(false));
12891289

1290-
DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) {
1290+
DiagnosticBehavior
1291+
DiagnosticState::determineBehavior(const Diagnostic &diag) const {
12911292
// We determine how to handle a diagnostic based on the following rules
12921293
// 1) Map the diagnostic to its "intended" behavior, applying the behavior
12931294
// limit for this particular emission
@@ -1334,21 +1335,23 @@ DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) {
13341335
if (suppressRemarks)
13351336
lvl = DiagnosticBehavior::Ignore;
13361337
}
1338+
return lvl;
1339+
}
13371340

1338-
// 5) Update current state for use during the next diagnostic
1339-
if (lvl == DiagnosticBehavior::Fatal) {
1341+
void DiagnosticState::updateFor(DiagnosticBehavior behavior) {
1342+
// Update current state for use during the next diagnostic
1343+
if (behavior == DiagnosticBehavior::Fatal) {
13401344
fatalErrorOccurred = true;
13411345
anyErrorOccurred = true;
1342-
} else if (lvl == DiagnosticBehavior::Error) {
1346+
} else if (behavior == DiagnosticBehavior::Error) {
13431347
anyErrorOccurred = true;
13441348
}
13451349

13461350
ASSERT((!AssertOnError || !anyErrorOccurred) && "We emitted an error?!");
1347-
ASSERT((!AssertOnWarning || (lvl != DiagnosticBehavior::Warning)) &&
1351+
ASSERT((!AssertOnWarning || (behavior != DiagnosticBehavior::Warning)) &&
13481352
"We emitted a warning?!");
13491353

1350-
previousBehavior = lvl;
1351-
return lvl;
1354+
previousBehavior = behavior;
13521355
}
13531356

13541357
void DiagnosticEngine::flushActiveDiagnostic() {
@@ -1393,6 +1396,8 @@ std::optional<DiagnosticInfo>
13931396
DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
13941397
bool includeDiagnosticName) {
13951398
auto behavior = state.determineBehavior(diagnostic);
1399+
state.updateFor(behavior);
1400+
13961401
if (behavior == DiagnosticBehavior::Ignore)
13971402
return std::nullopt;
13981403

lib/AST/Type.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,8 @@ GenericTypeParamType::GenericTypeParamType(GenericTypeParamKind paramKind,
21532153
const ASTContext &ctx)
21542154
: SubstitutableType(TypeKind::GenericTypeParam, &ctx, props),
21552155
Decl(nullptr) {
2156+
ASSERT(!(paramKind == GenericTypeParamKind::Value && !valueType) &&
2157+
"Value generic parameter must have type");
21562158
IsDecl = false;
21572159
Depth = depth;
21582160
Weight = weight;

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,10 @@ namespace {
17451745
}
17461746

17471747
Type visitTypeValueExpr(TypeValueExpr *E) {
1748-
return E->getParamDecl()->getValueType();
1748+
auto ty = E->getParamDecl()->getValueType();
1749+
if (ty->hasError())
1750+
return recordInvalidNode(E);
1751+
return ty;
17491752
}
17501753

17511754
Type visitDotSyntaxBaseIgnoredExpr(DotSyntaxBaseIgnoredExpr *expr) {

lib/Sema/TypeCheckExpr.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,30 @@ Expr *CallerSideDefaultArgExprRequest::evaluate(
774774
if (!TypeChecker::typeCheckParameterDefault(initExpr, dc, paramTy,
775775
param->isAutoClosure(),
776776
/*atCallerSide=*/true)) {
777-
if (param->hasDefaultExpr()) {
777+
auto isSimpleLiteral = [&]() -> bool {
778+
switch (param->getDefaultArgumentKind()) {
779+
#define MAGIC_IDENTIFIER(NAME, STRING) \
780+
case DefaultArgumentKind::NAME: return true;
781+
#include "swift/AST/MagicIdentifierKinds.def"
782+
case DefaultArgumentKind::NilLiteral:
783+
case DefaultArgumentKind::EmptyArray:
784+
case DefaultArgumentKind::EmptyDictionary:
785+
return true;
786+
default:
787+
return false;
788+
}
789+
};
790+
if (param->hasDefaultExpr() && isSimpleLiteral()) {
778791
// HACK: If we were unable to type-check the default argument in context,
779792
// then retry by type-checking it within the parameter decl, which should
780793
// also fail. This will present the user with a better error message and
781794
// allow us to avoid diagnosing on each call site.
795+
// Note we can't do this for expression macros since name lookup may
796+
// differ at the call side vs the declaration. We can however do it for
797+
// simple literals.
782798
transaction.abort();
783799
(void)param->getTypeCheckedDefaultExpr();
784-
assert(ctx.Diags.hadAnyError());
800+
ASSERT(ctx.Diags.hadAnyError());
785801
}
786802
return new (ctx) ErrorExpr(initExpr->getSourceRange(), paramTy);
787803
}

lib/Sema/TypeCheckRequestFunctions.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,12 @@ Type ResultBuilderTypeRequest::evaluate(Evaluator &evaluator,
486486
return type->mapTypeOutOfContext();
487487
}
488488

489-
Type GenericTypeParamDeclGetValueTypeRequest::evaluate(Evaluator &evaluator,
490-
GenericTypeParamDecl *decl) const {
491-
if (!decl->isValue())
492-
return Type();
489+
Type GenericTypeParamDeclGetValueTypeRequest::evaluate(
490+
Evaluator &evaluator, const GenericTypeParamDecl *decl) const {
491+
ASSERT(decl->isValue());
493492

494-
if (decl->getInherited().size() == 0) {
493+
auto inherited = decl->getInherited();
494+
if (inherited.empty()) {
495495
decl->diagnose(diag::missing_value_generic_type, decl->getName());
496496
return Type();
497497
}
@@ -502,12 +502,11 @@ Type GenericTypeParamDeclGetValueTypeRequest::evaluate(Evaluator &evaluator,
502502
//
503503
// We should have 1 inherited type for 'N', 'Int', and have a 2nd generic
504504
// parameter called 'Bool'.
505-
ASSERT(decl->getInherited().size() == 1);
505+
ASSERT(inherited.size() == 1);
506506

507507
// The value type of a generic parameter should never rely on the generic
508508
// signature of the generic parameter itself or any of the outside context.
509-
return decl->getInherited().getResolvedType(0,
510-
TypeResolutionStage::Structural);
509+
return inherited.getResolvedType(0, TypeResolutionStage::Structural);
511510
}
512511

513512
// Define request evaluation functions for each of the type checker requests.

lib/Sema/TypeCheckType.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,16 +808,15 @@ namespace {
808808
if (!paramType->isValue())
809809
return true;
810810

811-
// Both of these generic type parameters are values, but they may not have
812-
// underlying value types associated with them. This can occur when a
813-
// parameter doesn't declare a value type and we're going to diagnose it
814-
// later.
815-
if (!paramType->getValueType() || !secondType->getValueType())
811+
// If either value type has an error, we've already diagnosed the issue.
812+
auto paramValueTy = paramType->getValueType();
813+
auto secondValueTy = secondType->getValueType();
814+
if (paramValueTy->hasError() || secondValueTy->hasError())
816815
return true;
817816

818817
// Otherwise, these are both value parameters and check that both their
819818
// value types are the same.
820-
return paramType->getValueType()->isEqual(secondType->getValueType());
819+
return paramValueTy->isEqual(secondValueTy);
821820
}
822821

823822
bool alwaysMismatchTypeParameters() const { return true; }

0 commit comments

Comments
 (0)