Skip to content

[3.0] [Type checker] Don't overflow array bounds with #fileLiteral *ahem*. #4921

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
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
1 change: 1 addition & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
case KnownProtocolKind::ExpressibleByImageLiteral: index = 11; break;
case KnownProtocolKind::ExpressibleByFileReferenceLiteral: index = 12; break;
}
static_assert(NumAlternativeLiteralTypes == 13, "Wrong # of literal types");

// If we already looked for alternative literal types, return those results.
if (AlternativeLiteralTypes[index])
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,8 @@ class ConstraintSystem {
MemberLookups;

/// Cached sets of "alternative" literal types.
Optional<ArrayRef<Type>> AlternativeLiteralTypes[12];
static const unsigned NumAlternativeLiteralTypes = 13;
Optional<ArrayRef<Type>> AlternativeLiteralTypes[NumAlternativeLiteralTypes];

/// \brief Folding set containing all of the locators used in this
/// constraint system.
Expand Down
2 changes: 2 additions & 0 deletions test/Sema/object_literals_osx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ struct Path: _ExpressibleByFileReferenceLiteral {

let p1: Path = #fileLiteral(resourceName: "what.txt")
let p2 = #fileLiteral(resourceName: "what.txt") // expected-error{{could not infer type of file reference literal}} expected-note{{import Foundation to use 'URL' as the default file reference literal type}}

let text = #fileLiteral(resourceName: "TextFile.txt").relativeString! // expected-error{{type of expression is ambiguous without more context}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually ambiguous, or is this because there is no relativeString member in any of the things that #fileLiteral could be and we're emitting a misleading diagnostic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because there's no relativeString and we're emitting a misleading diagnostic. Actually, I think the default #fileLiteral type isn't even around; we get a different diagnostic about to having a relativeString member with the real SDK