Skip to content

Commit

Permalink
Fix name lookup bug, closes #683
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter committed Sep 15, 2023
1 parent c35da28 commit 99d66f0
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 11 deletions.
5 changes: 5 additions & 0 deletions regression-tests/pure2-deducing-pointers-error.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ main: () -> int = {
pa5 := pppa**;
pa5 = 0; // caught

// TODO: @filipsajdak please take a look
// The bugfix in get_declaration_of(t) to add `&& ri->position() <= t.position()`
// to the condition is correct; it fixes issue #669 by not looking past the first
// declaration of the name in t. However, that change made the following two
// "caught" cases no longer be caught.
fun(a)++; // caught
fp := fun(a);
fp = 0; // caught
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ pure2-deducing-pointers-error.cpp2(20,9): error: = - pointer assignment from nul
pure2-deducing-pointers-error.cpp2(21,9): error: += - pointer assignment from null or integer is illegal
pure2-deducing-pointers-error.cpp2(25,9): error: = - pointer assignment from null or integer is illegal
pure2-deducing-pointers-error.cpp2(29,9): error: = - pointer assignment from null or integer is illegal
pure2-deducing-pointers-error.cpp2(31,11): error: ++ - pointer arithmetic is illegal - use std::span or gsl::span instead
pure2-deducing-pointers-error.cpp2(33,8): error: = - pointer assignment from null or integer is illegal
==> program violates lifetime safety guarantee - see previous errors
==> program violates bounds safety guarantee - see previous errors

2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.2.1 Build 8914:0944
cppfront compiler v0.2.1 Build 8915:1445
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8914:0944"
"8915:1445"
63 changes: 63 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,9 @@ auto statement_node::visit(auto& v, int depth)
-> void
{
v.start(*this, depth);
if (parameters) {
parameters->visit(v, depth+1);
}
try_visit<expression >(statement, v, depth);
try_visit<compound >(statement, v, depth);
try_visit<selection >(statement, v, depth);
Expand Down Expand Up @@ -2360,6 +2363,8 @@ auto to_string(accessibility a)
}


struct declaration_identifier_tag { };

struct declaration_node
{
// The capture_group is declared first, because it should outlive
Expand Down Expand Up @@ -3193,9 +3198,11 @@ struct declaration_node
{
v.start(*this, depth);

v.start(declaration_identifier_tag{}, depth);
if (identifier) {
identifier->visit(v, depth+1);
}
v.end(declaration_identifier_tag{}, depth);

try_visit<a_function >(type, v, depth+1);
try_visit<an_object >(type, v, depth+1);
Expand Down Expand Up @@ -3645,6 +3652,8 @@ auto primary_expression_node::visit(auto& v, int depth)
}


struct next_expression_tag { };

auto iteration_statement_node::visit(auto& v, int depth)
-> void
{
Expand All @@ -3659,7 +3668,9 @@ auto iteration_statement_node::visit(auto& v, int depth)
statements->visit(v, depth+1);
}
if (next_expression) {
v.start(next_expression_tag{}, depth);
next_expression->visit(v, depth+1);
v.end(next_expression_tag{}, depth);
}
if (condition) {
assert(!range && !body);
Expand Down Expand Up @@ -3771,6 +3782,48 @@ struct translation_unit_node
};


//-----------------------------------------------------------------------
//
// print(*_node): pretty-prints Cpp2 ASTs
//
//-----------------------------------------------------------------------
//
//auto print(primary_expression_node const&)
// -> std::string
//{
// auto ret = std::string{};
// return ret;
//}
//auto print(literal_node const&)
//auto print(prefix_expression_node const&)
//auto print(binary_expression_node const&)
//auto print(expression_node const&)
//auto print(expression_list_node const&)
//auto print(expression_statement_node const&)
//auto print(postfix_expression_node const&)
//auto print(unqualified_id_node const&)
//auto print(qualified_id_node const&)
//auto print(type_id_node const&)
//auto print(is_as_expression_node const&)
//auto print(id_expression_node const&)
//auto print(compound_statement_node const&)
//auto print(selection_statement_node const&)
//auto print(iteration_statement_node const&)
//auto print(return_statement_node const&)
//auto print(alternative_node const&)
//auto print(inspect_expression_node const&)
//auto print(contract_node const&)
//auto print(jump_statement_node const&)
//auto print(statement_node const&)
//auto print(parameter_declaration_node const&)
//auto print(parameter_declaration_list_node const&)
//auto print(function_type_node const&)
//auto print(type_node const&)
//auto print(namespace_node const&)
//auto print(alias_node const&)
//auto print(declaration_node const&)


//-----------------------------------------------------------------------
//
// parser: parses a section of Cpp2 code
Expand Down Expand Up @@ -7589,6 +7642,16 @@ class parse_tree_printer : printing_visitor
o << pre(indent) << "template arguments\n";
}

auto start(declaration_identifier_tag const&, int indent) -> void
{
o << pre(indent) << "declaration identifier\n";
}

auto start(next_expression_tag const&, int indent) -> void
{
o << pre(indent) << "next expression\n";
}

auto start(alias_node const& n, int indent) -> void
{
o << pre(indent) << "alias\n";
Expand Down
6 changes: 3 additions & 3 deletions source/reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ auto first = true;
to_string += " return ret;\n}\n";
}

CPP2_UFCS(require, t, CPP2_UFCS(add_member, t, to_string),
CPP2_UFCS(require, t, CPP2_UFCS(add_member, t, std::move(to_string)),
"could not add to_string static function");
}
}
Expand Down Expand Up @@ -1652,7 +1652,7 @@ std::string comma = "";

#line 1128 "reflect.h2"
storage += " )> = ();\n";
CPP2_UFCS(require, t, CPP2_UFCS(add_member, t, storage),
CPP2_UFCS(require, t, CPP2_UFCS(add_member, t, std::move(storage)),
"could not add storage");
}
}
Expand Down Expand Up @@ -1709,7 +1709,7 @@ std::string destroy = " private destroy: (inout this) = {\n";
}

destroy += "}\n";
CPP2_UFCS(require, t, CPP2_UFCS(add_member, t, destroy),
CPP2_UFCS(require, t, CPP2_UFCS(add_member, t, std::move(destroy)),
"could not add destroy");
}
}
Expand Down
32 changes: 29 additions & 3 deletions source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ class sema
// Then look backward to find the first declaration of
// this name that is not deeper (in a nested scope)
// and is in the same function
for (auto ri = std::make_reverse_iterator(i+1); ri != symbols.crend(); ++ri )
for (
auto ri = std::make_reverse_iterator(i+1);
ri != symbols.crend() && ri->position() <= t.position(); // TODO: See pure2-deducing-pointers-error.cpp2
++ri
)
{
if (
ri->sym.index() == symbol::active::declaration
Expand Down Expand Up @@ -1448,11 +1452,23 @@ class sema
bool started_standalone_assignment_expression = false;
bool started_postfix_expression = false;
bool is_out_expression = false;
bool inside_next_expression = false;
bool inside_parameter_list = false;
bool inside_parameter_identifier = false;
bool inside_returns_list = false;
bool just_entered_for = false;
parameter_declaration_node const* inside_out_parameter = {};

auto start(next_expression_tag const&, int) -> void
{
inside_next_expression = true;
}

auto end(next_expression_tag const&, int) -> void
{
inside_next_expression = false;
}

auto start(parameter_declaration_list_node const&, int) -> void
{
inside_parameter_list = true;
Expand All @@ -1463,6 +1479,16 @@ class sema
inside_parameter_list = false;
}

auto start(declaration_identifier_tag const&, int) -> void
{
inside_parameter_identifier = inside_parameter_list;
}

auto end(declaration_identifier_tag const&, int) -> void
{
inside_parameter_identifier = false;
}

auto start(parameter_declaration_node const& n, int) -> void
{
if (
Expand Down Expand Up @@ -1598,12 +1624,12 @@ class sema
is_out_expression = false;
}

// Otherwise it's just an identifier use (if it's outside the parameter list) and
// Otherwise it's just an identifier use (if it's not a parameter name) and
// it's the first identifier of a postfix_expressions (not a member name or something else)
else if (started_postfix_expression)
{
started_postfix_expression = false;
if (!inside_parameter_list)
if (!inside_parameter_identifier && !inside_next_expression)
{
// Put this into the table if it's a use of an object in scope
// or it's a 'copy' parameter (but to be a use it must be after
Expand Down

0 comments on commit 99d66f0

Please sign in to comment.