Skip to content

[Parse] Disable support for multiline/extended escaping string literal in attribute message #19206

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 1 commit into from
Sep 10, 2018
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
6 changes: 5 additions & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,11 @@ ERROR(swift_native_objc_runtime_base_must_be_identifier,none,
"@_swift_native_objc_runtime_base class name must be an identifier", ())

ERROR(attr_interpolated_string,none,
"%0 cannot be an interpolated string literal", (StringRef))
"'%0' cannot be an interpolated string literal", (StringRef))
ERROR(attr_multiline_string,none,
"'%0' cannot be a multiline string literal", (StringRef))
ERROR(attr_extended_escaping_string,none,
"'%0' cannot be an extended escaping string literal", (StringRef))

ERROR(attr_only_at_non_local_scope, none,
"attribute '%0' can only be used in a non-local scope", (StringRef))
Expand Down
20 changes: 0 additions & 20 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,26 +2108,6 @@ StringRef Lexer::getEncodedStringSegment(StringRef Bytes,
// range check subscripting on the StringRef.
const char *BytesPtr = Bytes.begin();

// Special case when being called from EncodedDiagnosticMessage(...).
// This allows multiline and delimited strings to work in attributes.
// The string has already been validated by the initial parse.
if (IndentToStrip == ~0u && CustomDelimiterLen == ~0u) {
IndentToStrip = CustomDelimiterLen = 0;

// Restore trailing indent removal for multiline.
const char *Backtrack = BytesPtr - 1;
if (Backtrack[-1] == '"' && Backtrack[-2] == '"') {
Backtrack -= 2;
for (const char *Trailing = Bytes.end() - 1;
*Trailing == ' ' || *Trailing == '\t'; Trailing--)
IndentToStrip++;
}

// Restore delimiter if any.
while (*--Backtrack == '#')
CustomDelimiterLen++;
}

bool IsEscapedNewline = false;
while (BytesPtr < Bytes.end()) {
char CurChar = *BytesPtr++;
Expand Down
10 changes: 10 additions & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ bool Parser::parseTopLevel() {
static Optional<StringRef>
getStringLiteralIfNotInterpolated(Parser &P, SourceLoc Loc, const Token &Tok,
StringRef DiagText) {
// FIXME: Support extended escaping / multiline string literal.
if (Tok.getCustomDelimiterLen()) {
P.diagnose(Loc, diag::attr_extended_escaping_string, DiagText);
return None;
}
if (Tok.isMultilineString()) {
P.diagnose(Loc, diag::attr_multiline_string, DiagText);
return None;
}

SmallVector<Lexer::StringSegment, 1> Segments;
P.L->getStringLiteralSegments(Tok, Segments);
if (Segments.size() != 1 ||
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ class EncodedDiagnosticMessage {
public:
/// \param S A string with an encoded message
EncodedDiagnosticMessage(StringRef S)
: Message(Lexer::getEncodedStringSegment(S, Buf, true, true, ~0, ~0)) {}
: Message(Lexer::getEncodedStringSegment(S, Buf)) {}

/// The unescaped message to display to the user.
const StringRef Message;
Expand Down
19 changes: 19 additions & 0 deletions test/Parse/diagnose_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,22 @@ func swiftDeprecatedObsoleted() {}
// expected-warning@-1 {{expected 'introduced', 'deprecated', or 'obsoleted' in 'available' attribute for platform 'swift'}}
func swiftMessage() {}

@available(*, unavailable, message: "\("message")")
// expected-error@-1{{'message' cannot be an interpolated string literal}}
func interpolatedMessage() {}

// expected-error@+1{{'message' cannot be a multiline string literal}}
@available(*, unavailable, message: """
foobar message.
""")
func multilineMessage() {}

// expected-error@+1{{'message' cannot be an extended escaping string literal}}
@available(*, unavailable, message: #"""
foobar message.
"""#)
func extendedEscapedMultilineMessage() {}

// expected-error@+1{{'renamed' cannot be an extended escaping string literal}}
@available(*, unavailable, renamed: #"avialable()"#)
func extenedEscpaedRenamed() {}