Skip to content

Commit

Permalink
Add early resolver error collection
Browse files Browse the repository at this point in the history
We need to collect the early resolver's macros error to emit them at a
later stage after further expansion in order to retrieve macros defined
by other macro invocations.

gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc (Early::visit):
	Collect error instead of emitting it.
	* resolve/rust-early-name-resolver-2.0.h (std::function<void):
	Add type definition for collection.
	* rust-session-manager.cc (Session::expansion): Collect errors
	while fixed point is not reached then emit remaining errors.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
  • Loading branch information
P-E-P committed Aug 11, 2023
1 parent f7d9373 commit 95a11db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions gcc/rust/resolve/rust-early-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ Early::visit (AST::MacroInvocation &invoc)
// if the definition still does not have a value, then it's an error
if (!definition.has_value ())
{
rust_error_at (invoc.get_locus (), ErrorCode::E0433,
"could not resolve macro invocation");
collect_error ([&] () {
rust_error_at (invoc.get_locus (), ErrorCode::E0433,
"could not resolve macro invocation");
});
return;
}

Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/resolve/rust-early-name-resolver-2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
namespace Rust {
namespace Resolver2_0 {

using ResolveError = std::function<void ()>;

class Early : public DefaultResolver
{
using DefaultResolver::visit;
Expand All @@ -37,6 +39,11 @@ class Early : public DefaultResolver

void go (AST::Crate &crate);

const std::vector<ResolveError> &get_macro_resolve_errors () const
{
return macro_resolve_errors;
}

// we need to handle definitions for textual scoping
void visit (AST::MacroRulesDefinition &) override;

Expand Down Expand Up @@ -76,6 +83,9 @@ class Early : public DefaultResolver
};

TextualScope textual_scope;
std::vector<ResolveError> macro_resolve_errors;

void collect_error (ResolveError e) { macro_resolve_errors.push_back (e); }
};

} // namespace Resolver2_0
Expand Down
12 changes: 11 additions & 1 deletion gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ Session::expansion (AST::Crate &crate)
/* expand by calling cxtctxt object's monotonic_expander's expand_crate
* method. */
MacroExpander expander (crate, cfg, *this);
std::vector<Resolver2_0::ResolveError> macro_errors;

while (!fixed_point_reached && iterations < cfg.recursion_limit)
{
Expand All @@ -883,7 +884,11 @@ Session::expansion (AST::Crate &crate)
auto ctx = Resolver2_0::NameResolutionContext ();

if (flag_name_resolution_2_0)
Resolver2_0::Early (ctx).go (crate);
{
Resolver2_0::Early early (ctx);
early.go (crate);
macro_errors = early.get_macro_resolve_errors ();
}
else
Resolver::EarlyNameResolver ().go (crate);

Expand All @@ -897,6 +902,11 @@ Session::expansion (AST::Crate &crate)
break;
}

for (auto &error : macro_errors)
{
error ();
}

if (iterations == cfg.recursion_limit)
{
auto last_invoc = expander.get_last_invocation ();
Expand Down

0 comments on commit 95a11db

Please sign in to comment.