Skip to content

Commit ac60612

Browse files
committed
[Compile Time Values] Emit diagnostic note with parameter location on a non-@const argument to a @const parameter
1 parent 608e068 commit ac60612

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ERROR(require_const_initializer_for_const,none,
9090
"@const value should be initialized with a compile-time value", ())
9191

9292
ERROR(require_const_arg_for_parameter,none,
93-
"expected a compile-time value for a '@const' parameter", ())
93+
"expected a compile-time value argument for a '@const' parameter", ())
9494

9595
// FIXME: Used by both the parser and the type-checker.
9696
ERROR(func_decl_without_brace,PointsToFirstBadToken,

lib/SILOptimizer/Mandatory/DiagnoseUnknownCompileTimeValues.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
176176

177177
void verifyLocal(DebugValueInst *DBI) {
178178
auto Decl = DBI->getDecl();
179-
if (!Decl || !isa<VarDecl>(Decl) || isa<ParamDecl>(Decl) || !Decl->isConstVal())
179+
if (!Decl || !isa<VarDecl>(Decl) || isa<ParamDecl>(Decl) ||
180+
!Decl->isConstVal())
180181
return;
181182

182183
auto Value = ConstExprState.getConstantValue(DBI->getOperand());
@@ -228,14 +229,16 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
228229

229230
if (hasConst) {
230231
for (size_t i = 0; i < CalleeParameters->size(); ++i) {
231-
auto CorrespondingArg = ApplyArgRefs[i];
232-
if (CalleeParameters->get(i)->isConstVal()) {
232+
const auto &CorrespondingArg = ApplyArgRefs[i];
233+
const auto &CorrespondingParameter = CalleeParameters->get(i);
234+
if (CorrespondingParameter->isConstVal()) {
233235
LLVM_DEBUG({
234-
llvm::dbgs() << "Argument of fn{" << CalleeDecl->getNameStr() << "} ";
235-
llvm::dbgs() << CalleeParameters->get(i)->getNameStr() << ": ";
236+
llvm::dbgs() << "Argument of fn{" << CalleeDecl->getNameStr()
237+
<< "} ";
238+
llvm::dbgs() << CorrespondingParameter->getNameStr() << ": ";
236239
std::string typeName;
237240
llvm::raw_string_ostream out(typeName);
238-
CalleeParameters->get(i)->getTypeRepr()->print(out);
241+
CorrespondingParameter->getTypeRepr()->print(out);
239242
auto Value = ConstExprState.getConstantValue(CorrespondingArg);
240243
llvm::dbgs() << typeName << " = ";
241244
printSymbolicValueValue(Value, Allocator);
@@ -248,6 +251,10 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
248251
ArgLocation = ApplyExprNode->getArgs()[i].getLoc();
249252
getModule()->getASTContext().Diags.diagnose(
250253
ArgLocation, diag::require_const_arg_for_parameter);
254+
getModule()->getASTContext().Diags.diagnose(
255+
CorrespondingParameter->getLoc(),
256+
diag::kind_declname_declared_here, DescriptiveDeclKind::Param,
257+
CorrespondingParameter->getName());
251258
}
252259
}
253260
}

test/ConstValues/IntegerLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Constant globals on simple integer literals
22
// REQUIRES: swift_feature_CompileTimeValues
3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify -enable-experimental-feature CompileTimeValues
44

55
@const let constGlobal1: Int = 42
66
@const let constGlobal2: UInt = 42

test/ConstValues/Parameters.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Constant globals on simple integer literals
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-sil -primary-file %s -verify -enable-experimental-feature CompileTimeValues
4+
5+
func bar(@const _ thing: UInt) -> UInt {
6+
return thing
7+
}
8+
9+
func foo() {
10+
let _ = bar(42)
11+
let _ = bar(0xf000f000)
12+
#if _pointerBitWidth(_64)
13+
let _ = bar(0xf000f000_f000f000)
14+
#endif
15+
let _ = bar(UInt.random(in: 0..<10))
16+
// expected-error@-1 {{expected a compile-time value argument for a '@const' parameter}}
17+
// expected-note@-12 {{parameter 'thing' declared here}}
18+
}
19+

0 commit comments

Comments
 (0)