Skip to content

Commit fbf55ad

Browse files
committed
Don't emit general/vague errors if we already diagnosed something more specific
Makes error messages less noisy to read
1 parent 0d1544b commit fbf55ad

File tree

6 files changed

+167
-65
lines changed

6 files changed

+167
-65
lines changed
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
pure2-bounds-safety-pointer-arithmetic-error.cpp2...
2-
pure2-bounds-safety-pointer-arithmetic-error.cpp2(15,13): error: 'delete' and owning raw pointers are not supported in Cpp2
3-
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)
4-
pure2-bounds-safety-pointer-arithmetic-error.cpp2(9,25): error: invalid while loop body (at '{')
5-
pure2-bounds-safety-pointer-arithmetic-error.cpp2(3,3): error: ill-formed initializer (at '{')
6-
pure2-bounds-safety-pointer-arithmetic-error.cpp2(2,1): error: unexpected text at end of Cpp2 code section (at 'main')
7-
pure2-bounds-safety-pointer-arithmetic-error.cpp2(1,0): error: parse failed for section starting here
2+
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)
83

source/common.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,18 @@ struct error
236236
source_position where;
237237
std::string msg;
238238
bool internal = false;
239+
bool fallback = false; // only emit this message if there was nothing better
239240

240-
error( source_position w, std::string const& m, bool i = false )
241-
: where{w}, msg{m}, internal{i}
241+
error(
242+
source_position w,
243+
std::string const& m,
244+
bool i = false,
245+
bool f = false
246+
)
247+
: where{w}
248+
, msg{m}
249+
, internal{i}
250+
, fallback{f}
242251
{ }
243252

244253
auto operator==(error const& that)

source/cppfront.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,10 @@ class cppfront
952952
//
953953
cppfront(std::string const& filename)
954954
: sourcefile{ filename }
955-
, source{ errors }
956-
, tokens{ errors }
957-
, parser{ errors }
958-
, sema{ errors }
955+
, source { errors }
956+
, tokens { errors }
957+
, parser { errors }
958+
, sema { errors }
959959
{
960960
// "Constraints enable creativity in the right directions"
961961
// sort of applies here
@@ -998,7 +998,9 @@ class cppfront
998998
if (!parser.parse(entry, tokens.get_generated())) {
999999
errors.emplace_back(
10001000
source_position(line, 0),
1001-
"parse failed for section starting here"
1001+
"parse failed for section starting here",
1002+
false,
1003+
true // a noisy fallback error message
10021004
);
10031005
}
10041006
}
@@ -4454,7 +4456,7 @@ class cppfront
44544456
break;case passing_style::copy:
44554457
case passing_style::forward:
44564458
default:
4457-
errors.emplace_back( n.position(), "ICE: invalid parameter passing style, should have been rejected");
4459+
errors.emplace_back( n.position(), "ICE: invalid parameter passing style, should have been rejected", true);
44584460
}
44594461

44604462
switch (this_->mod) {
@@ -4844,8 +4846,20 @@ class cppfront
48444846
printer.abandon();
48454847
}
48464848

4847-
error const* prev = nullptr;
4848-
for (auto&& error : errors) {
4849+
error const* prev = {};
4850+
bool print_fallback_errors = true; // true until we find a non-fallback message
4851+
4852+
for (auto&& error : errors)
4853+
{
4854+
// Only print fallback error messages if we
4855+
// haven't found a better (non-fallback) one yet
4856+
if (!error.fallback) {
4857+
print_fallback_errors = false;
4858+
}
4859+
if (error.fallback && !print_fallback_errors) {
4860+
continue;
4861+
}
4862+
48494863
// Suppress adjacent duplicates (e.g., can arise when we
48504864
// reenter operator= to emit it as an assignment operator)
48514865
if (
@@ -4857,6 +4871,7 @@ class cppfront
48574871
}
48584872
prev = &error;
48594873
}
4874+
48604875
if (violates_lifetime_safety) {
48614876
std::cerr << " ==> program violates lifetime safety guarantee - see previous errors\n";
48624877
}

source/lex.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ auto expand_string_literal(
349349
if (text.back() != '"') {
350350
errors.emplace_back(
351351
source_position( src_pos ),
352-
"not a legal string literal"
352+
"not a legal string literal",
353+
false,
354+
true // a noisy fallback error message
353355
);
354356
return {};
355357
}
@@ -1551,7 +1553,9 @@ auto lex_line(
15511553
if (std::ssize(s) <= j + 1) {
15521554
errors.emplace_back(
15531555
source_position( lineno, i ),
1554-
"not a legal string literal"
1556+
"not a legal string literal",
1557+
false,
1558+
true // a noisy fallback error message
15551559
);
15561560
return {};
15571561
}
@@ -1632,17 +1636,13 @@ auto lex_line(
16321636
if (tokens.back() == "union") {
16331637
errors.emplace_back(
16341638
source_position(lineno, i),
1635-
"unsafe 'union's are not supported in Cpp2 - use std::variant instead"
1639+
"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)"
16361640
);
16371641
}
16381642
if (tokens.back() == "delete") {
16391643
errors.emplace_back(
16401644
source_position(lineno, i),
1641-
"'delete' and owning raw pointers are not supported in Cpp2"
1642-
);
1643-
errors.emplace_back(
1644-
source_position(lineno, i),
1645-
" - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)"
1645+
"'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)"
16461646
);
16471647
}
16481648
}
@@ -1653,7 +1653,9 @@ auto lex_line(
16531653
else if (!isspace(line[i])) {
16541654
errors.emplace_back(
16551655
source_position(lineno, i),
1656-
std::string("unexpected text '") + line[i] + "'"
1656+
std::string("unexpected text '") + line[i] + "'",
1657+
false,
1658+
true // a noisy fallback error message
16571659
);
16581660
}
16591661
}

source/load.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,9 @@ class source
957957
{
958958
errors.emplace_back(
959959
source_position(lineno_t(std::ssize(lines)), 0),
960-
std::string("unexpected error reading source lines - did not reach EOF")
960+
std::string("unexpected error reading source lines - did not reach EOF"),
961+
false,
962+
true // a noisy fallback error
961963
);
962964
return false;
963965
}

0 commit comments

Comments
 (0)