Skip to content

Commit 70072a0

Browse files
authored
Merge pull request #62122 from LucianoPAlmeida/fix-literal-dictionary-int
[Sema] printConstExprValue should consider negative signal when print number literals
2 parents d8a5fb5 + 4fb0e1c commit 70072a0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/AST/Expr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,12 @@ bool Expr::printConstExprValue(llvm::raw_ostream *OS,
215215
}
216216
case ExprKind::IntegerLiteral:
217217
case ExprKind::FloatLiteral: {
218-
auto digits = cast<NumberLiteralExpr>(E)->getDigitsText();
218+
const auto *NE = cast<NumberLiteralExpr>(E);
219+
auto digits = NE->getDigitsText();
219220
assert(!digits.empty());
221+
if (NE->getMinusLoc().isValid()) {
222+
print("-");
223+
}
220224
print(digits);
221225
return true;
222226
}

test/Sema/diag_dictionary_keys_duplicated.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,20 @@ let _: [Int: String] = [
227227
#column: "A", // expected-warning{{dictionary literal of type '[Int : String]' has duplicate entries for #column literal key}}
228228
#column: "B" // expected-note{{duplicate key declared here}} {{3-16=}} {{227:15-16=}}
229229
]
230+
231+
// https://github.com/apple/swift/issues/62117
232+
_ = [
233+
-1: "",
234+
1: "",
235+
]
236+
237+
_ = [
238+
-1.0: "",
239+
1.0: "",
240+
]
241+
242+
_ = [
243+
// expected-note@+1{{duplicate key declared}} {{3-9=}} {{9-10=}}
244+
-1: "", // expected-warning{{dictionary literal of type '[Int : String]' has duplicate entries for integer literal key '-1'}}
245+
-1: "", // expected-note{{duplicate key declared}} {{3-9=}} {{9-10=}}
246+
]

0 commit comments

Comments
 (0)