summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-09-27 11:28:03 +0800
committerJohn Hodge <tpg@mutabah.net>2016-09-27 11:28:03 +0800
commit9bbe1c9900441f5c678e282ed5efb0b1b638d2c0 (patch)
tree577ff0afb3d3a8a3248e36dfd9029776c153a20a
parentcfce816b251f4ba19c3fcc3ac53467c1a1159552 (diff)
downloadmrust-9bbe1c9900441f5c678e282ed5efb0b1b638d2c0.tar.gz
Resolve - Fix incorrect canonising of the index
-rw-r--r--src/resolve/absolute.cpp14
-rw-r--r--src/resolve/index.cpp62
2 files changed, 60 insertions, 16 deletions
diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp
index 8bb712dc..9e8856bc 100644
--- a/src/resolve/absolute.cpp
+++ b/src/resolve/absolute.cpp
@@ -248,6 +248,17 @@ struct Context
Pattern,
Variable,
};
+ static const char* lookup_mode_msg(LookupMode mode) {
+ switch(mode)
+ {
+ case LookupMode::Namespace: return "path component";
+ case LookupMode::Type: return "type name";
+ case LookupMode::Pattern: return "pattern name";
+ case LookupMode::Constant: return "constant name";
+ case LookupMode::Variable: return "variable name";
+ }
+ return "";
+ }
AST::Path lookup_mod(const Span& sp, const ::std::string& name) const {
return this->lookup(sp, name, LookupMode::Namespace);
}
@@ -1098,7 +1109,7 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex
// Set binding to binding of node in last module
::AST::Path tmp;
if( ! Context::lookup_in_mod(*mod, path_abs.nodes.back().name(), mode, tmp) ) {
- ERROR(sp, E0000, "Couldn't find path component '" << path_abs.nodes.back().name() << "' of " << path);
+ ERROR(sp, E0000, "Couldn't find " << Context::lookup_mode_msg(mode) << " '" << path_abs.nodes.back().name() << "' of " << path);
}
assert( ! tmp.binding().is_Unbound() );
@@ -1952,6 +1963,7 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod )
// - Run through the indexed items and fix up those paths
static Span sp;
+ DEBUG("mod = " << mod.path());
for(auto& i : mod.m_namespace_items) {
if( i.second.is_import ) {
Resolve_Absolute_Path(item_context, sp, Context::LookupMode::Namespace, i.second.path);
diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp
index 17a13c2a..4d66dd14 100644
--- a/src/resolve/index.cpp
+++ b/src/resolve/index.cpp
@@ -89,7 +89,7 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod)
for( const auto& i : mod.items() )
{
::AST::Path p = mod.path() + i.name;
- DEBUG("- p = " << p << " : " << ::AST::Item::tag_to_str(i.data.tag()));
+ //DEBUG("- p = " << p << " : " << ::AST::Item::tag_to_str(i.data.tag()));
TU_MATCH(AST::Item, (i.data), (e),
(None,
@@ -336,6 +336,10 @@ void Resolve_Index_Module_Wildcard__glob_in_hir_mod(const Span& sp, const AST::C
void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool handle_pub)
{
TRACE_FUNCTION_F("mod = " << mod.path() << ", handle_pub=" << handle_pub);
+ //if( mod.m_index_populated == 2 ) {
+ // DEBUG("- Index pre-populated")
+ // return ;
+ //}
// Glob/wildcard imports
for( const auto& i : mod.items() )
{
@@ -390,6 +394,8 @@ void Resolve_Index_Module_Wildcard(AST::Crate& crate, AST::Module& mod, bool han
// TODO: Handle wildcard import of a module with a public wildcard import
// TODO XXX: Huge chance of infinite recursion here (if the code recursively references)
Resolve_Index_Module_Wildcard(crate, *const_cast<AST::Module*>(e.module_), true);
+ assert(e.module_->m_index_populated == 2);
+ DEBUG("- Globbing in " << i_data.path);
}
for(const auto& vi : e.module_->m_namespace_items) {
if( vi.second.is_pub ) {
@@ -520,13 +526,15 @@ void Resolve_Index_Module_Normalise_Path_ext(const ::AST::Crate& crate, const Sp
ERROR(sp, E0000, "Couldn't find final node of path " << path);
}
-void Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span& sp, ::AST::Path& path)
+
+// Returns true if a change was made
+bool Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span& sp, ::AST::Path& path, IndexName loc)
{
const auto& info = path.m_class.as_Absolute();
if( info.crate != "" )
{
Resolve_Index_Module_Normalise_Path_ext(crate, sp, path, crate.m_extern_crates.at(info.crate), 0);
- return ;
+ return false;
}
const ::AST::Module* mod = &crate.m_root_module;
@@ -546,7 +554,7 @@ void Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span&
new_path.nodes().push_back( mv$(info.nodes[j]) );
new_path.bind( path.binding().clone() );
path = mv$(new_path);
- return Resolve_Index_Module_Normalise_Path(crate, sp, path);
+ return Resolve_Index_Module_Normalise_Path(crate, sp, path, loc);
}
else {
TU_MATCH_DEF(::AST::PathBinding, (ie.path.binding()), (e),
@@ -558,11 +566,11 @@ void Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span&
),
(Crate,
Resolve_Index_Module_Normalise_Path_ext(crate, sp, path, *e.crate_, i+1);
- return ;
+ return false;
),
(Enum,
// NOTE: Just assuming that if an Enum is hit, it's sane
- return ;
+ return false;
)
)
}
@@ -570,20 +578,40 @@ void Resolve_Index_Module_Normalise_Path(const ::AST::Crate& crate, const Span&
const auto& node = info.nodes.back();
- auto it = mod->m_namespace_items.find( node.name() );
- if( it == mod->m_namespace_items.end() )
- it = mod->m_value_items.find( node.name() );
- if( it == mod->m_value_items.end() )
+
+ // TODO: Use get_mod_index instead.
+ const ::AST::Module::IndexEnt* ie_p = nullptr;
+ switch(loc)
+ {
+ case IndexName::Namespace: {
+ auto it = mod->m_namespace_items.find( node.name() );
+ if( it != mod->m_namespace_items.end() )
+ ie_p = &it->second;
+ } break;
+ case IndexName::Value: {
+ auto it = mod->m_value_items.find( node.name() );
+ if( it != mod->m_value_items.end() )
+ ie_p = &it->second;
+ } break;
+ case IndexName::Type: {
+ auto it = mod->m_type_items.find( node.name() );
+ if( it != mod->m_type_items.end() )
+ ie_p = &it->second;
+ } break;
+ }
+ if( !ie_p )
ERROR(sp, E0000, "Couldn't find final node of path " << path);
- const auto& ie = it->second;
+ const auto& ie = *ie_p;
if( ie.is_import ) {
// TODO: Prevent infinite recursion if the user does something dumb
path = ::AST::Path(ie.path);
- Resolve_Index_Module_Normalise_Path(crate, sp, path);
+ Resolve_Index_Module_Normalise_Path(crate, sp, path, loc);
+ return true;
}
else {
// All good
+ return false;
}
}
void Resolve_Index_Module_Normalise(const ::AST::Crate& crate, const Span& mod_span, ::AST::Module& mod)
@@ -596,14 +624,18 @@ void Resolve_Index_Module_Normalise(const ::AST::Crate& crate, const Span& mod_s
)
}
+ DEBUG("Index for " << mod.path());
for( auto& ent : mod.m_namespace_items ) {
- Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path);
+ Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path, IndexName::Namespace);
+ DEBUG("NS " << ent.first << " = " << ent.second.path);
}
for( auto& ent : mod.m_type_items ) {
- Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path);
+ Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path, IndexName::Type);
+ DEBUG("Ty " << ent.first << " = " << ent.second.path);
}
for( auto& ent : mod.m_value_items ) {
- Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path);
+ Resolve_Index_Module_Normalise_Path(crate, mod_span, ent.second.path, IndexName::Value);
+ DEBUG("Val " << ent.first << " = " << ent.second.path);
}
}