Skip to content

Commit 53ca902

Browse files
committed
[Strict memory safety] Type alias types are only unsafe if their underlying type is
1 parent 8ec52c8 commit 53ca902

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

include/swift/AST/Types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ class RecursiveTypeProperties {
318318
Bits &= ~HasDependentMember;
319319
}
320320

321+
/// Remove the IsUnsafe property from this set.
322+
void removeIsUnsafe() {
323+
Bits &= ~IsUnsafe;
324+
}
325+
321326
/// Test for a particular property in this set.
322327
bool operator&(Property prop) const {
323328
return Bits & prop;

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,6 +3543,12 @@ TypeAliasType *TypeAliasType::get(TypeAliasDecl *typealias, Type parent,
35433543
auto &ctx = underlying->getASTContext();
35443544
auto arena = getArena(properties);
35453545

3546+
// Typealiases can't meaningfully be unsafe; it's the underlying type that
3547+
// matters.
3548+
properties.removeIsUnsafe();
3549+
if (underlying->isUnsafe())
3550+
properties |= RecursiveTypeProperties::IsUnsafe;
3551+
35463552
// Profile the type.
35473553
llvm::FoldingSetNodeID id;
35483554
TypeAliasType::Profile(id, typealias, parent, genericArgs, underlying);
@@ -4212,8 +4218,7 @@ static RecursiveTypeProperties getRecursivePropertiesAsParent(Type type) {
42124218

42134219
// Drop the "unsafe" bit. We have to recompute it without considering the
42144220
// enclosing nominal type.
4215-
properties = RecursiveTypeProperties(
4216-
properties.getBits() & ~static_cast<unsigned>(RecursiveTypeProperties::IsUnsafe));
4221+
properties.removeIsUnsafe();
42174222

42184223
// Check generic arguments of parent types.
42194224
while (type) {

lib/Sema/TypeCheckUnsafe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
377377
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
378378
return;
379379

380-
if (!type->getCanonicalType()->isUnsafe())
380+
if (!type->isUnsafe())
381381
return;
382382

383383
// Look for a specific @unsafe nominal type along the way.

0 commit comments

Comments
 (0)