Files
swift-mirror/unittests/AST/ASTDumperTests.cpp
T
Kavon Farvardin 21b2ebff12 Sema: infer inverses in opaque return types
Opaque return types are special type declarations that have it
own nested generic signature. Thus, given this:
```
  protocol P<A> { associatedtype A: ~Copyable }
  func f<T: ~Copyable>() -> some P<T> {}
```
The generic signature for f is <T where T Escapable>, and
for the opaque return type, its nested signature ends up as
```
  <X where X: P, X.A == T>
```
With SE-503, we will now also expand a default for the suppressed
primary associated type, so the signature after expansion becomes
```
  <X where X: P, X.A == T, X.A: Copyable>
```
It would be smarter to effectively have this rule
```
  X.A == T, T: ~Copyable
  ----------------------
     X.A: ~Copyable
```
where we infer the inverse on X.A to cancel-out the
expanded default X.A: Copyable. We already do this for
two in-scope type parameters, and it would be better if
we did it if one side was out-of-scope, but that would
be source-breaking to do in general.

In the case of opaque return types, the fact that
it has a nested generic signature seems more an
artifact of the implementation. There also is little
risk of source break, as the only kinds of same-type
requirements that can appear are from parameterized
protocol type.

The experimental suppressed associated types prior to
SE-503 wouldn't be broken by this change, as they do
not infer defaults that need suppression, and we only
filter-out requirements from defaults expansion, rather
than explicitly-written ones.

rdar://175500824
2026-05-04 14:49:34 -07:00

57 lines
1.7 KiB
C++

//===--- ASTDumperTests.cpp - Tests for ASTDumper -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "TestContext.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Types.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <string>
using namespace swift;
using namespace swift::unittest;
TEST(ASTDumper, ArchetypeType) {
TestContext C;
auto &ctx = C.Ctx;
auto sig = buildGenericSignature(ctx, nullptr, {ctx.TheSelfType}, {},
ExpandDefaults);
TypeBase *archetype = nullptr;
{
llvm::SmallVector<ProtocolDecl *> protocols;
archetype = PrimaryArchetypeType::getNew(ctx, sig.getGenericEnvironment(),
ctx.TheSelfType, protocols, Type(),
nullptr);
}
std::string fullStr;
{
llvm::raw_string_ostream os(fullStr);
archetype->dump(os);
}
llvm::StringRef str(fullStr);
EXPECT_TRUE(str.consume_front("(primary_archetype_type address=0x"));
{
intptr_t integer;
EXPECT_FALSE(str.consumeInteger(16, integer));
}
EXPECT_EQ(str,
" name=\"τ_0_0\"\n"
" (interface_type=generic_type_param_type depth=0 index=0 param_kind=type))\n");
}