Skip to content

Commit

Permalink
Add diagnostics for sometype. and object::, closes #339
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter committed Apr 9, 2023
1 parent be42e33 commit 5fa2707
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
42 changes: 42 additions & 0 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,27 @@ class cppfront
auto emit(qualified_id_node const& n)
-> void
{
// Check for some incorrect uses of :: or .
if (auto decl = sema.get_declaration_of(n.get_first_token(), true);
decl && std::ssize(n.ids) > 1
)
{
assert (decl->declaration);

if (
decl->declaration->is_object()
&& n.ids[1].scope_op
&& n.ids[1].scope_op->type() == lexeme::Scope
)
{
errors.emplace_back(
n.position(),
"use '" + decl->identifier->to_string(true) + ".' to refer to an object member"
);
return;
}
}

// Implicit "cpp2::" qualification of "unique.new" and "shared.new"
if (
n.ids.size() == 2
Expand Down Expand Up @@ -2536,6 +2557,27 @@ class cppfront
assert(n.expr);
last_postfix_expr_was_pointer = false;

// Check for some incorrect uses of :: or .
if (auto decl = sema.get_declaration_of(n.get_first_token_ignoring_this(), true);
decl && !n.ops.empty()
)
{
assert (decl->declaration);

if (
decl->declaration->is_type()
&& n.ops[0].op
&& n.ops[0].op->type() == lexeme::Dot
)
{
errors.emplace_back(
n.position(),
"use '" + decl->identifier->to_string(true) + "::' to refer to a type member"
);
return;
}
}

// For a 'move that' parameter, track the members we already moved from
// so we can diagnose attempts to move from the same member twice
if (
Expand Down
10 changes: 10 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,16 @@ struct qualified_id_node
return {};
}

auto get_first_token() const
-> token const*
{
assert (
!ids.empty()
&& ids.front().id
);
return ids.front().id->get_token();
}

auto position() const
-> source_position
{
Expand Down
12 changes: 12 additions & 0 deletions source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ class sema

// Get the declaration of t within the same named function or beyond it
//
auto get_declaration_of(
token const* t,
bool look_beyond_current_function = false
)
-> declaration_sym const*
{
if (!t) {
return {};
}
return get_declaration_of(*t, look_beyond_current_function);
}

auto get_declaration_of(
token const& t,
bool look_beyond_current_function = false
Expand Down

0 comments on commit 5fa2707

Please sign in to comment.