diff options
author | John Hodge <tpg@ucc.asn.au> | 2019-05-18 17:38:05 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2019-05-18 17:38:05 +0800 |
commit | dab5cf5462d8fce6d6fcaa1343df8f5f3b763b8a (patch) | |
tree | 89ea3e68050872e2f462dd783c55cddb26d2b5c2 /src | |
parent | 8183bf1f7ffa47cd966bdc7872ca36875d9d83a1 (diff) | |
download | mrust-dab5cf5462d8fce6d6fcaa1343df8f5f3b763b8a.tar.gz |
HIR - Fix privacy error for items in non-library crate root
Diffstat (limited to 'src')
-rw-r--r-- | src/ast/dump.cpp | 2 | ||||
-rw-r--r-- | src/hir/hir.cpp | 39 | ||||
-rw-r--r-- | src/hir/hir.hpp | 37 |
3 files changed, 43 insertions, 35 deletions
diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index f0bdfa11..00848fb5 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -1042,7 +1042,7 @@ void RustPrinter::handle_struct(const AST::Struct& s) (Tuple, m_os << "("; for( const auto& i : e.ents ) - m_os << i.m_type << ", "; + m_os << (i.m_is_public ? "pub " : "") << i.m_type << ", "; m_os << ")\n"; print_bounds(s.params()); m_os << indent() << ";\n"; diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp index 4b9aef10..2a8ac8bf 100644 --- a/src/hir/hir.cpp +++ b/src/hir/hir.cpp @@ -17,6 +17,20 @@ #include <mir/main_bindings.hpp> namespace HIR { + ::std::ostream& operator<<(::std::ostream& os, const Publicity& x) + { + if( !x.vis_path ) { + os << "pub"; + } + else if( *x.vis_path == *Publicity::none_path ) { + os << "priv"; + } + else { + os << "pub(" << *x.vis_path << ")"; + } + return os; + } + ::std::ostream& operator<<(::std::ostream& os, const ::HIR::Literal& v) { TU_MATCH(::HIR::Literal, (v), (e), @@ -110,6 +124,31 @@ namespace HIR { } } +::std::shared_ptr<::HIR::SimplePath> HIR::Publicity::none_path = ::std::make_shared<HIR::SimplePath>(::HIR::SimplePath{"#", {}}); + +bool HIR::Publicity::is_visible(const ::HIR::SimplePath& p) const +{ + // No path = global public + if( !vis_path ) + return true; + // Empty simple path = full private + if( *vis_path == *none_path ) { + return false; + } + // Crate names must match + if(p.m_crate_name != vis_path->m_crate_name) + return false; + // `p` must be a child of vis_path + if(p.m_components.size() < vis_path->m_components.size()) + return false; + for(size_t i = 0; i < vis_path->m_components.size(); i ++) + { + if(p.m_components[i] != vis_path->m_components[i]) + return false; + } + return true; +} + size_t HIR::Enum::find_variant(const ::std::string& name) const { if( m_data.is_Value() ) diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index 6c8385c4..cc66a9e7 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -43,6 +43,7 @@ class ItemPath; class Publicity { + static ::std::shared_ptr<::HIR::SimplePath> none_path; ::std::shared_ptr<::HIR::SimplePath> vis_path; Publicity(::std::shared_ptr<::HIR::SimplePath> p) @@ -55,7 +56,6 @@ public: return Publicity({}); } static Publicity new_none() { - static ::std::shared_ptr<::HIR::SimplePath> none_path = ::std::make_shared<HIR::SimplePath>(); return Publicity(none_path); } static Publicity new_priv(::HIR::SimplePath p) { @@ -65,40 +65,9 @@ public: bool is_global() const { return !vis_path; } - bool is_visible(const ::HIR::SimplePath& p) const { - // No path = global public - if( !vis_path ) - return true; - // Empty simple path = full private - if( *vis_path == ::HIR::SimplePath() ) { - return false; - } - // Crate names must match - if(p.m_crate_name != vis_path->m_crate_name) - return false; - // `p` must be a child of vis_path - if(p.m_components.size() < vis_path->m_components.size()) - return false; - for(size_t i = 0; i < vis_path->m_components.size(); i ++) - { - if(p.m_components[i] != vis_path->m_components[i]) - return false; - } - return true; - } + bool is_visible(const ::HIR::SimplePath& p) const; - friend ::std::ostream& operator<<(::std::ostream& os, const Publicity& x) { - if( !x.vis_path ) { - os << "pub"; - } - else if( *x.vis_path == ::HIR::SimplePath() ) { - os << "priv"; - } - else { - os << "pub(" << *x.vis_path << ")"; - } - return os; - } + friend ::std::ostream& operator<<(::std::ostream& os, const Publicity& x); }; template<typename Ent> |