Skip to content

Commit b21e768

Browse files
committed
[Sema] make printf a reserved function
1 parent 24ad78b commit b21e768

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

include/lexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum class TokenKind : char {
3434
KwWhile,
3535
KwReturn,
3636
KwStruct,
37+
KwPrintf,
3738

3839
Eof = singleCharTokens[0],
3940
Lpar = singleCharTokens[1],

src/sema.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ Sema::resolveFunctionDecl(const FunctionDecl &function) {
666666
if (!function.params.empty())
667667
return report(function.location,
668668
"'main' function is expected to take no arguments");
669+
} else if (function.identifier == "printf") {
670+
return report(function.location,
671+
"'printf' is a reserved function name and cannot be used for "
672+
"user-defined functions");
669673
}
670674

671675
std::vector<std::unique_ptr<ResolvedParamDecl>> resolvedParams;

test/sema/printf_reserved.yl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: compiler %s -res-dump 2>&1 | filecheck %s
2+
3+
// CHECK: [[# @LINE + 1 ]]:1: error: 'printf' is a reserved function name and cannot be used for user-defined functions
4+
fn printf(): number {
5+
return 12345;
6+
}
7+
8+
fn main(): void {
9+
println(printf());
10+
}

0 commit comments

Comments
 (0)