dlang / project-ideas

Collection of impactful projects in the D ecosystem
36 stars 12 forks source link

Add improve d error messages project #101

Closed RazvanN7 closed 5 months ago

RazvanN7 commented 1 year ago

Description

D's error messages are of sufficient quality to get by, however they are fairly uninspiring compared to what is available in other languages with similar aims (C++, Rust etc).

A short example Let's compare the error messages due to this nonsensical function in D, and then when transliterated into forms other compilers understand.

int square(int num)
{
    return num * num + "chimp";
    //This exact operation was chosen so C++ would also give an error
    //e.g. num + 0.0 is relatively kosher in the eye's of even clang
}

D says: (3): Error: incompatible types for (num * num) + ("chimp") int and string

Rust Says:

error[E0277]: cannot add `&str` to `i32`
 --> <source>:3:15
  |
3 |     num * num + "chimp";
  |               ^ no implementation for `i32 + &str`
  |
  = help: the trait `Add<&str>` is not implemented for `i32`

GCC Says:

<source>: In function 'int square(int)':
<source>:3:22: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
    3 |     return num * num + "chimp";
      |            ~~~~~~~~~~^~~~~~~~~
      |                      |
      |                      const char*

From these examples we can identify three things that can be improved

D should provide an annotated quotation of the offending code. D should attempt to be a little more prosaic in the text of it's messages, that is, we find that we can't do x because of y, then we proceed to print y but not x D should get into the habit (more on this further down) of providing information in a structured manner. This extends beyond aesthetics - to take an example directly from programming, good code doesn't need indentation because it's pretty but rather because it communicates at an almost subconscious level what the flow of the code does. These problems get worse as the code get's deeper - i.e. it's easy for template error messages like

(3): Error: incompatible types for (num * num) + ("chimp"): int and string (6): Error: template instance example.square!int error instantiating to get lost in a soup of error message output.

In this case, the compiler needs a mechanism to represent an Error (message) in the abstract as it goes through the compiler. Currently, the basic currency the frontend deals with is just the string

nothrow void error(ref const Loc loc, const(char)* format, ...);

This is clearly not good enough in 2023. There are areas of the compiler which effectively build their formatting code (like template constraints) - while greatly appreciated these changes are not an effective model for the future.

What are rough milestones of this project?

This should be fairly generic code, so it can be reused in less common paths like having two quotations in one error.

An arbitrary target: No D compiler code should use explicit formatting for errors (e.g. \n) eventually.

These later steps are more for laying down a framework that can easily be added to than full completion.

How does this project help the D community?

More informative error messages serve two main purposes, the first is to make explicit what the compiler considers to be an error and where, and the second is to aid newcomers to the D programming language by pointing them in the right direction as to how to fix their code.

Recommended Skills

What can students expect to get out of doing this project?

Point of contact

@maxhaton @RazvanN7