Skip to content

Commit

Permalink
Don't emit general/vague errors if we already diagnosed something mor…
Browse files Browse the repository at this point in the history
…e specific

Makes error messages less noisy to read
  • Loading branch information
hsutter committed Mar 23, 2023
1 parent 0d1544b commit fbf55ad
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
pure2-bounds-safety-pointer-arithmetic-error.cpp2...
pure2-bounds-safety-pointer-arithmetic-error.cpp2(15,13): error: 'delete' and owning raw pointers are not supported in Cpp2
pure2-bounds-safety-pointer-arithmetic-error.cpp2(15,13): error: - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)
pure2-bounds-safety-pointer-arithmetic-error.cpp2(9,25): error: invalid while loop body (at '{')
pure2-bounds-safety-pointer-arithmetic-error.cpp2(3,3): error: ill-formed initializer (at '{')
pure2-bounds-safety-pointer-arithmetic-error.cpp2(2,1): error: unexpected text at end of Cpp2 code section (at 'main')
pure2-bounds-safety-pointer-arithmetic-error.cpp2(1,0): error: parse failed for section starting here
pure2-bounds-safety-pointer-arithmetic-error.cpp2(15,13): error: 'delete' and owning raw pointers are not supported in Cpp2 - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)

13 changes: 11 additions & 2 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,18 @@ struct error
source_position where;
std::string msg;
bool internal = false;
bool fallback = false; // only emit this message if there was nothing better

error( source_position w, std::string const& m, bool i = false )
: where{w}, msg{m}, internal{i}
error(
source_position w,
std::string const& m,
bool i = false,
bool f = false
)
: where{w}
, msg{m}
, internal{i}
, fallback{f}
{ }

auto operator==(error const& that)
Expand Down
31 changes: 23 additions & 8 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,10 +952,10 @@ class cppfront
//
cppfront(std::string const& filename)
: sourcefile{ filename }
, source{ errors }
, tokens{ errors }
, parser{ errors }
, sema{ errors }
, source { errors }
, tokens { errors }
, parser { errors }
, sema { errors }
{
// "Constraints enable creativity in the right directions"
// sort of applies here
Expand Down Expand Up @@ -998,7 +998,9 @@ class cppfront
if (!parser.parse(entry, tokens.get_generated())) {
errors.emplace_back(
source_position(line, 0),
"parse failed for section starting here"
"parse failed for section starting here",
false,
true // a noisy fallback error message
);
}
}
Expand Down Expand Up @@ -4454,7 +4456,7 @@ class cppfront
break;case passing_style::copy:
case passing_style::forward:
default:
errors.emplace_back( n.position(), "ICE: invalid parameter passing style, should have been rejected");
errors.emplace_back( n.position(), "ICE: invalid parameter passing style, should have been rejected", true);
}

switch (this_->mod) {
Expand Down Expand Up @@ -4844,8 +4846,20 @@ class cppfront
printer.abandon();
}

error const* prev = nullptr;
for (auto&& error : errors) {
error const* prev = {};
bool print_fallback_errors = true; // true until we find a non-fallback message

for (auto&& error : errors)
{
// Only print fallback error messages if we
// haven't found a better (non-fallback) one yet
if (!error.fallback) {
print_fallback_errors = false;
}
if (error.fallback && !print_fallback_errors) {
continue;
}

// Suppress adjacent duplicates (e.g., can arise when we
// reenter operator= to emit it as an assignment operator)
if (
Expand All @@ -4857,6 +4871,7 @@ class cppfront
}
prev = &error;
}

if (violates_lifetime_safety) {
std::cerr << " ==> program violates lifetime safety guarantee - see previous errors\n";
}
Expand Down
20 changes: 11 additions & 9 deletions source/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ auto expand_string_literal(
if (text.back() != '"') {
errors.emplace_back(
source_position( src_pos ),
"not a legal string literal"
"not a legal string literal",
false,
true // a noisy fallback error message
);
return {};
}
Expand Down Expand Up @@ -1551,7 +1553,9 @@ auto lex_line(
if (std::ssize(s) <= j + 1) {
errors.emplace_back(
source_position( lineno, i ),
"not a legal string literal"
"not a legal string literal",
false,
true // a noisy fallback error message
);
return {};
}
Expand Down Expand Up @@ -1632,17 +1636,13 @@ auto lex_line(
if (tokens.back() == "union") {
errors.emplace_back(
source_position(lineno, i),
"unsafe 'union's are not supported in Cpp2 - use std::variant instead"
"unsafe 'union's are not supported in Cpp2 - use std::variant instead (or, in the future, the Cpp2 'union' metaclass function, but that is not yet implemented)"
);
}
if (tokens.back() == "delete") {
errors.emplace_back(
source_position(lineno, i),
"'delete' and owning raw pointers are not supported in Cpp2"
);
errors.emplace_back(
source_position(lineno, i),
" - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)"
"'delete' and owning raw pointers are not supported in Cpp2 - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)"
);
}
}
Expand All @@ -1653,7 +1653,9 @@ auto lex_line(
else if (!isspace(line[i])) {
errors.emplace_back(
source_position(lineno, i),
std::string("unexpected text '") + line[i] + "'"
std::string("unexpected text '") + line[i] + "'",
false,
true // a noisy fallback error message
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion source/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,9 @@ class source
{
errors.emplace_back(
source_position(lineno_t(std::ssize(lines)), 0),
std::string("unexpected error reading source lines - did not reach EOF")
std::string("unexpected error reading source lines - did not reach EOF"),
false,
true // a noisy fallback error
);
return false;
}
Expand Down
Loading

0 comments on commit fbf55ad

Please sign in to comment.