Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nr2 unit struct #3299

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 2 additions & 4 deletions gcc/rust/backend/rust-compile-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ class CompileItem : private HIRCompileBase, protected HIR::HIRStmtVisitor
public:
static tree compile (HIR::Item *item, Context *ctx,
TyTy::BaseType *concrete = nullptr,
bool is_query_mode = false,
location_t ref_locus = UNDEF_LOCATION)
{
CompileItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);

if (is_query_mode && compiler.reference == error_mark_node)
rust_internal_error_at (ref_locus, "failed to compile item: %s",
item->as_string ().c_str ());
if (compiler.reference == error_mark_node)
rust_debug ("failed to compile item: %s", item->as_string ().c_str ());

return compiler.reference;
}
Expand Down
11 changes: 7 additions & 4 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
}
}

// Handle unit struct
if (lookup->get_kind () == TyTy::TypeKind::ADT)
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);

// let the query system figure it out
tree resolved_item = query_compile (ref, lookup, final_segment, mappings,
expr_locus, is_qualified_path);
Expand All @@ -206,11 +211,9 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
{
if (!lookup->has_substitutions_defined ())
return CompileItem::compile (*resolved_item, ctx, nullptr, true,
expr_locus);
return CompileItem::compile (*resolved_item, ctx, nullptr, expr_locus);
else
return CompileItem::compile (*resolved_item, ctx, lookup, true,
expr_locus);
return CompileItem::compile (*resolved_item, ctx, lookup, expr_locus);
}
else if (auto hir_extern_item
= ctx->get_mappings ().lookup_hir_extern_item (ref))
Expand Down
14 changes: 7 additions & 7 deletions gcc/rust/resolve/rust-ast-resolve-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ ResolveExpr::visit (AST::IfLetExpr &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// We know expr.get_patterns () has one pattern at most
// so there's no reason to handle it like an AltPattern.
Expand Down Expand Up @@ -278,7 +278,7 @@ ResolveExpr::visit (AST::IfLetExprConseqElse &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// We know expr.get_patterns () has one pattern at most
// so there's no reason to handle it like an AltPattern.
Expand Down Expand Up @@ -307,7 +307,7 @@ ResolveExpr::visit (AST::BlockExpr &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

if (expr.has_label ())
{
Expand Down Expand Up @@ -615,7 +615,7 @@ ResolveExpr::visit (AST::ForLoopExpr &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// resolve the expression
PatternDeclaration::go (expr.get_pattern (), Rib::ItemType::Var);
Expand Down Expand Up @@ -681,7 +681,7 @@ ResolveExpr::visit (AST::MatchExpr &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// resolve
AST::MatchArm &arm = match_case.get_arm ();
Expand Down Expand Up @@ -750,7 +750,7 @@ ResolveExpr::visit (AST::ClosureExprInner &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

std::vector<PatternBinding> bindings
= {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())};
Expand Down Expand Up @@ -780,7 +780,7 @@ ResolveExpr::visit (AST::ClosureExprInnerTyped &expr)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

std::vector<PatternBinding> bindings
= {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())};
Expand Down
10 changes: 5 additions & 5 deletions gcc/rust/resolve/rust-ast-resolve-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ResolveTraitItems::visit (AST::Function &function)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

if (function.has_generics ())
for (auto &generic : function.get_generic_params ())
Expand Down Expand Up @@ -216,7 +216,7 @@ ResolveItem::visit (AST::Module &module)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// FIXME: Should we reinsert a child here? Any reason we ResolveTopLevel::go
// in ResolveTopLevel::visit (AST::Module) as well as here?
Expand Down Expand Up @@ -473,7 +473,7 @@ ResolveItem::visit (AST::Function &function)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

if (function.has_generics ())
for (auto &generic : function.get_generic_params ())
Expand Down Expand Up @@ -648,7 +648,7 @@ ResolveItem::visit (AST::TraitImpl &impl_block)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

if (impl_block.has_generics ())
for (auto &generic : impl_block.get_generic_params ())
Expand Down Expand Up @@ -1053,7 +1053,7 @@ ResolveExternItem::visit (AST::Function &function)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// resolve the generics
if (function.has_generics ())
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class ResolveStmt : public ResolverBase
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

if (function.has_generics ())
for (auto &generic : function.get_generic_params ())
Expand Down
23 changes: 15 additions & 8 deletions gcc/rust/resolve/rust-ast-resolve-toplevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,21 @@ class ResolveTopLevel : public ResolverBase
auto path = prefix.append (decl);
auto cpath = canonical_prefix.append (decl);

resolver->get_type_scope ().insert (
path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
Rib::ItemType::Type,
[&] (const CanonicalPath &, NodeId, location_t locus) -> void {
rich_location r (line_table, struct_decl.get_locus ());
r.add_range (locus);
rust_error_at (r, "redefined multiple times");
});
auto duplicate_item
= [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
rich_location r (line_table, struct_decl.get_locus ());
r.add_range (locus);
rust_error_at (r, "redefined multiple times");
};

resolver->get_type_scope ().insert (path, struct_decl.get_node_id (),
struct_decl.get_locus (), false,
Rib::ItemType::Type, duplicate_item);

if (struct_decl.is_unit_struct ())
resolver->get_name_scope ().insert (path, struct_decl.get_node_id (),
struct_decl.get_locus (), false,
Rib::ItemType::Type, duplicate_item);

NodeId current_module = resolver->peek_current_module_scope ();
mappings.insert_module_child_item (current_module, decl);
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ NameResolution::go (AST::Crate &crate)
resolver->get_label_scope ().push (scope_node_id);
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_label_scope ().peek ());

// get the root segment
CanonicalPath crate_prefix
Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/resolve/rust-forever-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ template <Namespace N> class ForeverStack
tl::optional<Rib &> to_rib (NodeId rib_id);
tl::optional<const Rib &> to_rib (NodeId rib_id) const;

std::string as_debug_string ();
std::string as_debug_string () const;

/**
* Used to check if a module is a descendant of another module
Expand Down Expand Up @@ -752,9 +752,9 @@ template <Namespace N> class ForeverStack
std::reference_wrapper<Node> cursor_reference;

void stream_rib (std::stringstream &stream, const Rib &rib,
const std::string &next, const std::string &next_next);
const std::string &next, const std::string &next_next) const;
void stream_node (std::stringstream &stream, unsigned indentation,
const Node &node);
const Node &node) const;

/* Helper types and functions for `resolve_path` */

Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/resolve/rust-forever-stack.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ template <Namespace N>
void
ForeverStack<N>::stream_rib (std::stringstream &stream, const Rib &rib,
const std::string &next,
const std::string &next_next)
const std::string &next_next) const
{
if (rib.get_values ().empty ())
{
Expand All @@ -718,7 +718,7 @@ ForeverStack<N>::stream_rib (std::stringstream &stream, const Rib &rib,
template <Namespace N>
void
ForeverStack<N>::stream_node (std::stringstream &stream, unsigned indentation,
const ForeverStack<N>::Node &node)
const ForeverStack<N>::Node &node) const
{
auto indent = std::string (indentation, ' ');
auto next = std::string (indentation + 4, ' ');
Expand Down Expand Up @@ -750,7 +750,7 @@ ForeverStack<N>::stream_node (std::stringstream &stream, unsigned indentation,

template <Namespace N>
std::string
ForeverStack<N>::as_debug_string ()
ForeverStack<N>::as_debug_string () const
{
std::stringstream stream;

Expand Down
35 changes: 35 additions & 0 deletions gcc/rust/resolve/rust-name-resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,41 @@ class Resolver
void insert_captured_item (NodeId id);
const std::set<NodeId> &get_captures (NodeId id) const;

std::string as_debug_string () const
{
std::stringstream ss;

ss << "Names:\n";
for (auto &n : name_ribs)
{
ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
<< "\n";
}
ss << "Types:\n";
for (auto &n : type_ribs)
{
ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
<< "\n";
}
ss << "Macros:\n";

for (auto &n : macro_ribs)
{
ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
<< "\n";
}

ss << "Labels:\n";

for (auto &n : label_ribs)
{
ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
<< "\n";
}

return ss.str ();
}

protected:
bool decl_needs_capture (NodeId decl_rib_node_id, NodeId closure_rib_node_id,
const Scope &scope);
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/typecheck/rust-hir-trait-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
}
else
{
ok = resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
&ref);
auto path_nodeid = path.get_mappings ().get_nodeid ();
ok = resolver->lookup_resolved_type (path_nodeid, &ref)
|| resolver->lookup_resolved_name (path_nodeid, &ref)
|| resolver->lookup_resolved_macro (path_nodeid, &ref);
}

if (!ok)
Expand Down
9 changes: 5 additions & 4 deletions gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item)
rust_assert (canonical_path.has_value ());

RustIdent ident{*canonical_path, item.get_locus ()};
variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
item.get_mappings ().get_defid (),
item.get_identifier ().as_string (), ident,
item.take_discriminant_expression ());
variant
= new TyTy::VariantDef (item.get_mappings ().get_hirid (),
item.get_mappings ().get_defid (),
item.get_identifier ().as_string (), ident,
item.get_discriminant_expression ().clone_expr ());
}

void
Expand Down
Loading
Loading