From 686e4a600cd1c076b2a6b698adbdd54247ce6617 Mon Sep 17 00:00:00 2001 From: Owen Voorhees Date: Sun, 4 Aug 2019 17:57:54 -0700 Subject: [PATCH] Assign a SourceLoc to implicit appendInterpolation calls for diagnostics This helps ensure that if, for example, a deprecated appendInterpolation is used, the resulting diag points to the opening paren of the corresponding string interpolation segment --- lib/Parse/ParseExpr.cpp | 10 ++++---- ...diag_deprecated_string_interpolation.swift | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/Sema/diag_deprecated_string_interpolation.swift diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index b04f7e9185280..a7fc42efce400 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -1855,11 +1855,11 @@ parseStringSegments(SmallVectorImpl &Segments, TokReceiver->registerTokenKindChange(Tok.getLoc(), tok::string_interpolation_anchor); - auto callee = new (Context) UnresolvedDotExpr(InterpolationVarRef, - /*dotloc=*/BackSlashLoc, - appendInterpolation, - /*nameloc=*/DeclNameLoc(), - /*Implicit=*/true); + auto callee = new (Context) + UnresolvedDotExpr(InterpolationVarRef, + /*dotloc=*/BackSlashLoc, appendInterpolation, + /*nameloc=*/DeclNameLoc(Segment.Loc), + /*Implicit=*/true); auto S = parseExprCallSuffix(makeParserResult(callee), true); // If we stopped parsing the expression before the expression segment is diff --git a/test/Sema/diag_deprecated_string_interpolation.swift b/test/Sema/diag_deprecated_string_interpolation.swift new file mode 100644 index 0000000000000..61aa2944d3b6d --- /dev/null +++ b/test/Sema/diag_deprecated_string_interpolation.swift @@ -0,0 +1,24 @@ +// RUN: %target-swift-frontend -swift-version 5 -typecheck %s 2>&1 | %FileCheck %s + +extension DefaultStringInterpolation { + @available(*, deprecated) func appendInterpolation(deprecated: Int) {} +} + +// Make sure diagnostics emitted via string interpolations have a reasonable source location + +_ = "\(deprecated: 42)" +// CHECK: [[@LINE-1]]:7: warning: 'appendInterpolation(deprecated:)' is deprecated + +_ = "hello, world\(deprecated: 42)!!!" +// CHECK: [[@LINE-1]]:19: warning: 'appendInterpolation(deprecated:)' is deprecated + +_ = "\(42)\(deprecated: 42)test\(deprecated: 42)" +// CHECK: [[@LINE-1]]:12: warning: 'appendInterpolation(deprecated:)' is deprecated +// CHECK: [[@LINE-2]]:33: warning: 'appendInterpolation(deprecated:)' is deprecated + +_ = """ +This is a multiline literal with a deprecated interpolation: + +\(deprecated: 42) +""" +// CHECK: [[@LINE-2]]:2: warning: 'appendInterpolation(deprecated:)' is deprecated