@@ -4084,7 +4084,7 @@ Module::load_items ()
inner_attrs = parser.parse_inner_attributes ();
auto parsed_items = parser.parse_items ();
for (const auto &error : parser.get_errors ())
- error.emit_error ();
+ error.emit ();
items = std::move (parsed_items);
kind = ModuleKind::LOADED;
@@ -63,7 +63,7 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction,
NodeId ref_node_id = UNKNOWN_NODEID;
if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
{
- invalid_path.emit_error ();
+ invalid_path.emit ();
return false;
}
// FIXME: Add a hint here if we can find the path in another scope, such as
@@ -77,7 +77,7 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction,
auto module = mappings.lookup_module (ref);
if (!module)
{
- invalid_path.emit_error ();
+ invalid_path.emit ();
return false;
}
@@ -736,7 +736,7 @@ MacroBuiltin::include_handler (Location invoc_locus, AST::MacroInvocData &invoc)
bool has_error = !parser.get_errors ().empty ();
for (const auto &error : parser.get_errors ())
- error.emit_error ();
+ error.emit ();
if (has_error)
{
@@ -848,7 +848,7 @@ parse_many (Parser<MacroInvocLexer> &parser, TokenId &delimiter,
if (node.is_error ())
{
for (auto err : parser.get_errors ())
- err.emit_error ();
+ err.emit ();
return AST::Fragment::create_error ();
}
@@ -991,7 +991,7 @@ transcribe_type (Parser<MacroInvocLexer> &parser)
auto type = parser.parse_type (true);
for (auto err : parser.get_errors ())
- err.emit_error ();
+ err.emit ();
auto end = lexer.get_offs ();
@@ -444,7 +444,7 @@ Parser<ManagedTokenSource>::parse_crate ()
// emit all errors
for (const auto &error : error_table)
- error.emit_error ();
+ error.emit ();
return std::unique_ptr<AST::Crate> (
new AST::Crate (std::move (items), std::move (inner_attrs)));
@@ -231,14 +231,49 @@ rust_debug_loc (const Location location, const char *fmt, ...)
}
namespace Rust {
-Error::Error (const Location location, const char *fmt, ...) : locus (location)
+
+/**
+ * This function takes ownership of `args` and calls `va_end` on it
+ */
+static Error
+va_constructor (Error::Kind kind, Location locus, const char *fmt, va_list args)
+ RUST_ATTRIBUTE_GCC_DIAG (3, 0);
+
+static Error
+va_constructor (Error::Kind kind, Location locus, const char *fmt, va_list args)
+{
+ std::string message = expand_message (fmt, args);
+ message.shrink_to_fit ();
+ va_end (args);
+
+ return Error (kind, locus, message);
+}
+
+Error::Error (const Location location, const char *fmt, ...)
+ : kind (Kind::Err), locus (location)
{
va_list ap;
+ va_start (ap, fmt);
+ *this = va_constructor (Kind::Err, location, fmt, ap);
+}
+
+Error
+Error::Hint (const Location location, const char *fmt, ...)
+{
+ va_list ap;
va_start (ap, fmt);
- message = expand_message (fmt, ap);
- va_end (ap);
- message.shrink_to_fit ();
+ return va_constructor (Kind::Hint, location, fmt, ap);
+}
+
+Error
+Error::Fatal (const Location location, const char *fmt, ...)
+{
+ va_list ap;
+ va_start (ap, fmt);
+
+ return va_constructor (Kind::FatalErr, location, fmt, ap);
}
+
} // namespace Rust
@@ -114,27 +114,69 @@ namespace Rust {
* errors to be ignored, e.g. if backtracking. */
struct Error
{
+ enum class Kind
+ {
+ Hint,
+ Err,
+ FatalErr,
+ };
+
+ Kind kind;
Location locus;
std::string message;
// TODO: store more stuff? e.g. node id?
- Error (Location locus, std::string message)
- : locus (locus), message (std::move (message))
+ Error (Kind kind, Location locus, std::string message)
+ : kind (kind), locus (locus), message (std::move (message))
{
message.shrink_to_fit ();
}
+ Error (Location locus, std::string message)
+ {
+ Error (Kind::Err, locus, std::move (message));
+ }
+
+ static Error Hint (Location locus, std::string message)
+ {
+ return Error (Kind::Hint, locus, std::move (message));
+ }
+
+ static Error Fatal (Location locus, std::string message)
+ {
+ return Error (Kind::FatalErr, locus, std::move (message));
+ }
+
// TODO: the attribute part might be incorrect
Error (Location locus, const char *fmt,
...) /*RUST_ATTRIBUTE_GCC_DIAG (2, 3)*/ RUST_ATTRIBUTE_GCC_DIAG (3, 4);
- // Irreversibly emits the error as an error.
- void emit_error () const { rust_error_at (locus, "%s", message.c_str ()); }
+ /**
+ * printf-like overload of Error::Hint
+ */
+ static Error Hint (Location locus, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3);
+
+ /**
+ * printf-like overload of Error::Fatal
+ */
+ static Error Fatal (Location locus, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3);
- // Irreversibly emits the error as a fatal error.
- void emit_fatal_error () const
+ void emit () const
{
- rust_fatal_error (locus, "%s", message.c_str ());
+ switch (kind)
+ {
+ case Kind::Hint:
+ rust_inform (locus, "%s", message.c_str ());
+ break;
+ case Kind::Err:
+ rust_error_at (locus, "%s", message.c_str ());
+ break;
+ case Kind::FatalErr:
+ rust_fatal_error (locus, "%s", message.c_str ());
+ break;
+ }
}
};
} // namespace Rust
@@ -193,7 +193,7 @@ Session::handle_option (
else
{
rust_assert (!error.message.empty ());
- error.emit_error ();
+ error.emit ();
}
}
else
@@ -391,7 +391,7 @@ Session::handle_crate_name (const AST::Crate &parsed_crate)
if (!validate_crate_name (msg_str, error))
{
error.locus = attr.get_locus ();
- error.emit_error ();
+ error.emit ();
continue;
}
@@ -412,7 +412,7 @@ Session::handle_crate_name (const AST::Crate &parsed_crate)
if (!options.crate_name_set_manually
&& !validate_crate_name (options.crate_name, error))
{
- error.emit_error ();
+ error.emit ();
rust_inform (linemap->get_location (0),
"crate name inferred from this file");
}