Skip to content

Canonicalize different spellings of the same integer generic parameter. #80519

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
2 changes: 1 addition & 1 deletion include/swift/AST/TypeNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ TYPE(PackExpansion, Type)
TYPE(PackElement, Type)
UNCHECKED_TYPE(TypeVariable, Type)
UNCHECKED_TYPE(ErrorUnion, Type)
ALWAYS_CANONICAL_TYPE(Integer, Type)
TYPE(Integer, Type)
ABSTRACT_SUGARED_TYPE(Sugar, Type)
SUGARED_TYPE(TypeAlias, SugarType)
SUGARED_TYPE(Locatable, SugarType)
Expand Down
23 changes: 21 additions & 2 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -7884,10 +7884,27 @@ class IntegerType final : public TypeBase, public llvm::FoldingSetNode {
friend class ASTContext;

StringRef Value;
// Integers may not be canonical, but don't have any structural type
// components from which to get the ASTContext, so we need to store a
// reference to it ourselves.
const ASTContext &Context;

static const ASTContext *
getCanonicalIntegerLiteralContext(StringRef value, const ASTContext &ctx) {
for (char c : value) {
// A canonical integer literal consists only of ASCII decimal digits.
if (c < '0' || c > '9') {
return nullptr;
}
}
return &ctx;
}

IntegerType(StringRef value, bool isNegative, const ASTContext &ctx) :
TypeBase(TypeKind::Integer, &ctx, RecursiveTypeProperties()),
Value(value) {
TypeBase(TypeKind::Integer, getCanonicalIntegerLiteralContext(value, ctx),
RecursiveTypeProperties()),
Value(value),
Context(ctx) {
Bits.IntegerType.IsNegative = isNegative;
}

Expand Down Expand Up @@ -7917,6 +7934,8 @@ class IntegerType final : public TypeBase, public llvm::FoldingSetNode {
static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::Integer;
}

const ASTContext &getASTContext() { return Context; }
};
DEFINE_EMPTY_CAN_TYPE_WRAPPER(IntegerType, Type)

Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3724,8 +3724,9 @@ IntegerType *IntegerType::get(StringRef value, bool isNegative,
IntegerType::Profile(id, value, isNegative);

void *insertPos;
if (auto intType = ctx.getImpl().IntegerTypes.FindNodeOrInsertPos(id, insertPos))
if (auto intType = ctx.getImpl().IntegerTypes.FindNodeOrInsertPos(id, insertPos)) {
return intType;
}

auto strCopy = ctx.AllocateCopy(value);

Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6532,6 +6532,7 @@ namespace {
printCommon("integer_type", label);
printFlag(T->isNegative(), "is_negative");
printFieldQuoted(T->getValue(), Label::always("value"), LiteralValueColor);
printFieldQuoted(T->getDigitsText(), Label::always("text"), IdentifierColor);
printFoot();
}

Expand Down
12 changes: 12 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,18 @@ CanType TypeBase::computeCanonicalType() {
Result = ErrorUnionType::get(ctx, newTerms).getPointer();
break;
}
case TypeKind::Integer: {
auto intTy = cast<IntegerType>(this);
APInt value = intTy->getValue();
if (intTy->isNegative()) {
value = -value;
}
SmallString<20> canonicalText;
value.toStringUnsigned(canonicalText);
Result = IntegerType::get(canonicalText, intTy->isNegative(),
intTy->getASTContext());
break;
}
}

// Cache the canonical type for future queries.
Expand Down
15 changes: 15 additions & 0 deletions test/Sema/integer_generics_spelling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -disable-availability-checking -typecheck -verify %s

struct Foo<let n: Int> {}

func foo(x: Foo<256>) {}

func bar(x: Foo<0x100>) {
foo(x: x)
}

func oof(x: Foo<-256>) {}

func rab(x: Foo<-0x100>) {
oof(x: x)
}