Skip to content
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

Providing a more accurate message when trying to use a reserved word as a function name #1750

Closed
andoni-hazaiah opened this issue Dec 31, 2024 · 4 comments
Assignees
Labels
Accepted Accepted Request Enhancement Request New feature or request Implemented Needs Verification Check if this issue is resolved
Milestone

Comments

@andoni-hazaiah
Copy link

Hi 👋!

When the code is compiled, the compiler says Error: A function name must start with lower case, as names starting with upper case are reserved for types., pointing to double function name. But it's not accurate for our situation where we're trying to use a reserved type keyword.

Sample code:

module arrays;

import std::io;

fn void double() {}

fn void main() {}

saying something like "Cannot use the reserved type 'double' as a function name" instead of the current misleading message about case sensitivity would be more helpful.

In parse_func_macro_header, when it sees this token type and a left parenthesis, it assumes it's trying to define a function with an uppercase name (which is reserved for types).

The error message could be improved by:

  • a. Adding a specific check for built-in type tokens in parse_func_macro_header
  • b. Providing a more accurate error message when trying to use a reserved keyword as a function name as above.
@lerno lerno added Enhancement Request New feature or request Accepted Accepted Request labels Dec 31, 2024
@lerno lerno self-assigned this Dec 31, 2024
@lerno lerno added the Implemented Needs Verification Check if this issue is resolved label Dec 31, 2024
@lerno
Copy link
Collaborator

lerno commented Dec 31, 2024

This should now be fixed.

@lerno lerno added this to the 0.6.6 milestone Dec 31, 2024
@andoni-hazaiah
Copy link
Author

Here's a suggested improvement to the code in parse_global.c, I couldn’t test it though:

if (method_type->kind == TYPE_INFO_IDENTIFIER) {
    if (tok_is(c, TOKEN_LPAREN)) {
        // Check if it's a built-in type token
        if (method_type->type >= TOKEN_VOID && method_type->type <= TOKEN_TYPEID) {
            RETURN_PRINT_ERROR_AT(false, method_type, "Cannot use the reserved type '%s' as a function name.", token_type_to_string(method_type->type));
        }
        RETURN_PRINT_ERROR_AT(false, method_type, "A function name must start with lower case, as names starting with upper case are reserved for types.");
    }
    RETURN_PRINT_ERROR_AT(false, method_type, "There is unexpectedly a type after the return type, did you forget a '.'?");
}

This change would:

  1. First check if we're trying to use a built-in type token as a function name
  2. If so, provide a more specific error message mentioning the reserved type name
  3. Otherwise, fall back to the existing error messages

@lerno
Copy link
Collaborator

lerno commented Jan 1, 2025

Was there a problem with the solution I wrote?

@lerno lerno closed this as completed Jan 17, 2025
@andoni-hazaiah
Copy link
Author

Was there a problem with the solution I wrote?

Hey there!

I apologize for the delay.

I was able to see it only after posting my response.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Accepted Request Enhancement Request New feature or request Implemented Needs Verification Check if this issue is resolved
Projects
None yet
Development

No branches or pull requests

2 participants