Skip to content

Commit 42b0427

Browse files
committed
[Sema] Rework TypeExpr folding for placeholders
1 parent aabeb8d commit 42b0427

File tree

4 files changed

+242
-74
lines changed

4 files changed

+242
-74
lines changed

include/swift/AST/Expr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ class alignas(8) Expr {
558558
/// the parent map.
559559
llvm::DenseMap<Expr *, Expr *> getParentMap();
560560

561+
/// Whether this expression is a valid parent for a TypeExpr.
562+
bool isValidTypeExprParent() const;
563+
561564
SWIFT_DEBUG_DUMP;
562565
void dump(raw_ostream &OS, unsigned Indent = 0) const;
563566
void dump(raw_ostream &OS, llvm::function_ref<Type(Expr *)> getType,

lib/AST/Expr.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,130 @@ llvm::DenseMap<Expr *, Expr *> Expr::getParentMap() {
742742
return parentMap;
743743
}
744744

745+
bool Expr::isValidTypeExprParent() const {
746+
// Allow references to types as a part of:
747+
// - member references T.foo, T.Type, T.self, etc.
748+
// - constructor calls T()
749+
// - Subscripts T[]
750+
//
751+
// This is an exhaustive list of the accepted syntactic forms.
752+
switch (getKind()) {
753+
case ExprKind::Error:
754+
case ExprKind::DotSelf:
755+
case ExprKind::Call:
756+
case ExprKind::MemberRef:
757+
case ExprKind::UnresolvedMember:
758+
case ExprKind::DotSyntaxCall:
759+
case ExprKind::ConstructorRefCall:
760+
case ExprKind::UnresolvedDot:
761+
case ExprKind::DotSyntaxBaseIgnored:
762+
case ExprKind::UnresolvedSpecialize:
763+
case ExprKind::OpenExistential:
764+
case ExprKind::Subscript:
765+
return true;
766+
767+
case ExprKind::NilLiteral:
768+
case ExprKind::BooleanLiteral:
769+
case ExprKind::IntegerLiteral:
770+
case ExprKind::FloatLiteral:
771+
case ExprKind::StringLiteral:
772+
case ExprKind::MagicIdentifierLiteral:
773+
case ExprKind::InterpolatedStringLiteral:
774+
case ExprKind::ObjectLiteral:
775+
case ExprKind::DiscardAssignment:
776+
case ExprKind::DeclRef:
777+
case ExprKind::SuperRef:
778+
case ExprKind::Type:
779+
case ExprKind::OtherConstructorDeclRef:
780+
case ExprKind::OverloadedDeclRef:
781+
case ExprKind::UnresolvedDeclRef:
782+
case ExprKind::DynamicMemberRef:
783+
case ExprKind::DynamicSubscript:
784+
case ExprKind::Sequence:
785+
case ExprKind::Paren:
786+
case ExprKind::Await:
787+
case ExprKind::UnresolvedMemberChainResult:
788+
case ExprKind::Try:
789+
case ExprKind::ForceTry:
790+
case ExprKind::OptionalTry:
791+
case ExprKind::Tuple:
792+
case ExprKind::Array:
793+
case ExprKind::Dictionary:
794+
case ExprKind::KeyPathApplication:
795+
case ExprKind::TupleElement:
796+
case ExprKind::CaptureList:
797+
case ExprKind::Closure:
798+
case ExprKind::AutoClosure:
799+
case ExprKind::InOut:
800+
case ExprKind::VarargExpansion:
801+
case ExprKind::DynamicType:
802+
case ExprKind::RebindSelfInConstructor:
803+
case ExprKind::OpaqueValue:
804+
case ExprKind::PropertyWrapperValuePlaceholder:
805+
case ExprKind::AppliedPropertyWrapper:
806+
case ExprKind::DefaultArgument:
807+
case ExprKind::BindOptional:
808+
case ExprKind::OptionalEvaluation:
809+
case ExprKind::ForceValue:
810+
case ExprKind::MakeTemporarilyEscapable:
811+
case ExprKind::PrefixUnary:
812+
case ExprKind::PostfixUnary:
813+
case ExprKind::Binary:
814+
case ExprKind::Load:
815+
case ExprKind::DestructureTuple:
816+
case ExprKind::UnresolvedTypeConversion:
817+
case ExprKind::FunctionConversion:
818+
case ExprKind::CovariantFunctionConversion:
819+
case ExprKind::CovariantReturnConversion:
820+
case ExprKind::ImplicitlyUnwrappedFunctionConversion:
821+
case ExprKind::MetatypeConversion:
822+
case ExprKind::CollectionUpcastConversion:
823+
case ExprKind::Erasure:
824+
case ExprKind::AnyHashableErasure:
825+
case ExprKind::BridgeToObjC:
826+
case ExprKind::BridgeFromObjC:
827+
case ExprKind::ConditionalBridgeFromObjC:
828+
case ExprKind::DerivedToBase:
829+
case ExprKind::ArchetypeToSuper:
830+
case ExprKind::InjectIntoOptional:
831+
case ExprKind::ClassMetatypeToObject:
832+
case ExprKind::ExistentialMetatypeToObject:
833+
case ExprKind::ProtocolMetatypeToObject:
834+
case ExprKind::InOutToPointer:
835+
case ExprKind::ArrayToPointer:
836+
case ExprKind::StringToPointer:
837+
case ExprKind::PointerToPointer:
838+
case ExprKind::ForeignObjectConversion:
839+
case ExprKind::UnevaluatedInstance:
840+
case ExprKind::UnderlyingToOpaque:
841+
case ExprKind::DifferentiableFunction:
842+
case ExprKind::LinearFunction:
843+
case ExprKind::DifferentiableFunctionExtractOriginal:
844+
case ExprKind::LinearFunctionExtractOriginal:
845+
case ExprKind::LinearToDifferentiableFunction:
846+
case ExprKind::ForcedCheckedCast:
847+
case ExprKind::ConditionalCheckedCast:
848+
case ExprKind::Is:
849+
case ExprKind::Coerce:
850+
case ExprKind::Arrow:
851+
case ExprKind::If:
852+
case ExprKind::EnumIsCase:
853+
case ExprKind::Assign:
854+
case ExprKind::CodeCompletion:
855+
case ExprKind::UnresolvedPattern:
856+
case ExprKind::LazyInitializer:
857+
case ExprKind::EditorPlaceholder:
858+
case ExprKind::ObjCSelector:
859+
case ExprKind::KeyPath:
860+
case ExprKind::KeyPathDot:
861+
case ExprKind::OneWay:
862+
case ExprKind::Tap:
863+
return false;
864+
}
865+
866+
llvm_unreachable("Unhandled ExprKind in switch.");
867+
}
868+
745869
//===----------------------------------------------------------------------===//
746870
// Support methods for Exprs.
747871
//===----------------------------------------------------------------------===//

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -620,25 +620,9 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
620620
// literal since it used to be accepted.
621621
DiagnosticBehavior behavior = DiagnosticBehavior::Error;
622622

623-
// Allow references to types as a part of:
624-
// - member references T.foo, T.Type, T.self, etc.
625-
// - constructor calls T()
626-
// - Subscripts T[]
627623
if (auto *ParentExpr = Parent.getAsExpr()) {
628-
// This is an exhaustive list of the accepted syntactic forms.
629-
if (isa<ErrorExpr>(ParentExpr) ||
630-
isa<DotSelfExpr>(ParentExpr) || // T.self
631-
isa<CallExpr>(ParentExpr) || // T()
632-
isa<MemberRefExpr>(ParentExpr) || // T.foo
633-
isa<UnresolvedMemberExpr>(ParentExpr) ||
634-
isa<SelfApplyExpr>(ParentExpr) || // T.foo() T()
635-
isa<UnresolvedDotExpr>(ParentExpr) ||
636-
isa<DotSyntaxBaseIgnoredExpr>(ParentExpr) ||
637-
isa<UnresolvedSpecializeExpr>(ParentExpr) ||
638-
isa<OpenExistentialExpr>(ParentExpr) ||
639-
isa<SubscriptExpr>(ParentExpr)) {
624+
if (ParentExpr->isValidTypeExprParent())
640625
return;
641-
}
642626

643627
if (!Ctx.LangOpts.isSwiftVersionAtLeast(6)) {
644628
auto argument = CallArgs.find(ParentExpr);

0 commit comments

Comments
 (0)