Merge pull request #2523 from AleoHQ/fix/issue-2521

[Fix] Function calls must lead with an `Identifier.
This commit is contained in:
d0cd 2023-08-13 00:09:38 -04:00 committed by GitHub
commit a601ccd5cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 2 deletions

View File

@ -15,9 +15,11 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use leo_errors::{ParserError, Result};
use leo_span::{sym, Symbol};
use snarkvm_console::{account::Address, network::Testnet3};
const INT_TYPES: &[Token] = &[
@ -444,6 +446,10 @@ impl ParserContext<'_> {
// Eat a core struct constant or core struct function call.
expr = self.parse_associated_access_expression(expr)?;
} else if self.check(&Token::LeftParen) {
// Check that the expression is an identifier.
if !matches!(expr, Expression::Identifier(_)) {
self.emit_err(ParserError::unexpected(expr.to_string(), "an identifier", expr.span()))
}
// Parse a function call that's by itself.
let (arguments, _, span) = self.parse_paren_comma_list(|p| p.parse_expression().map(Some))?;
expr = Expression::Call(CallExpression {

View File

@ -456,7 +456,7 @@ impl<'a> CodeGenerator<'a> {
// Lookup the function return type.
let function_name = match input.function.borrow() {
Expression::Identifier(identifier) => identifier.name,
_ => unreachable!("Parsing guarantees that all `input.function` is always an identifier."),
_ => unreachable!("Parsing guarantees that a function name is always an identifier."),
};
let return_type = &self.symbol_table.borrow().lookup_fn_symbol(function_name).unwrap().output_type;
match return_type {

View File

@ -453,6 +453,8 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
}
fn visit_call(&mut self, input: &'a CallExpression, expected: &Self::AdditionalInput) -> Self::Output {
println!("call_expression: {}", input);
println!("input function: {:?}", input.function);
match &*input.function {
// Note that the parser guarantees that `input.function` is always an identifier.
Expression::Identifier(ident) => {
@ -514,7 +516,7 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
None
}
}
_ => unreachable!("Parser guarantees that `input.function` is always an identifier."),
_ => unreachable!("Parsing guarantees that a function name is always an identifier."),
}
}

View File

@ -0,0 +1,5 @@
---
namespace: Parse
expectation: Fail
outputs:
- "Error [EPAR0370005]: expected an identifier -- found '100u8'\n --> test:5:16\n |\n 5 | return 100u8(0u8);\n | ^^^^^"

View File

@ -0,0 +1,10 @@
/*
namespace: Parse
expectation: Fail
*/
program test.aleo {
function x(x: u32, constant y: i32) -> u8 {
return 100u8(0u8);
}
}