[Bug c++/92270] New: Non-const ref treated as const-ref in the compiler.

2019-10-29 Thread jostaberry.jam at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92270

Bug ID: 92270
   Summary: Non-const ref treated as const-ref in the compiler.
   Product: gcc
   Version: 7.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: jostaberry.jam at gmail dot com
  Target Milestone: ---

GCC rejects this valid code, but it can be compiled without "i = 0;".
And as expected, Overload #1 is selected.

=== CODE ===
#include 
#include 

template 
struct test {
T value;

template 
std::enable_if_t>
fn(F&& op) & { op(value); } // #1

template 
std::enable_if_t>
fn(F&& op) const& { op(value); } // #2
};

template  test(T) -> test;

int main() {
test instance{1};
instance.fn([](auto& i){
// error: cannot assign to variable 'i'
// with const-qualified type 'const int &'
i = 0; 
});
}
= END CODE =




source>:24:11: error: cannot assign to variable 'i' with const-qualified type
'const int &'

i = 0; 

~ ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2283:7:
note: in instantiation of function template specialization 'main()::(anonymous
class)::operator()' requested here

  std::declval<_Fn>()(std::declval<_Args>()...)

  ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2294:24:
note: while substituting deduced template arguments into function template
'_S_test' [with _Fn = (lambda at :21:17) &&, _Args = (no value)]

  typedef decltype(_S_test<_Functor, _ArgTypes...>(0)) type;

   ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2300:14:
note: in instantiation of template class 'std::__result_of_impl:21:17) &&, const int &>' requested here

: public __result_of_impl<

 ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2612:65:
note: in instantiation of template class 'std::__invoke_result<(lambda at
:21:17) &&, const int &>' requested here

struct __is_invocable_impl<_Result, _Ret, __void_t>

^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2696:7:
note: during template argument deduction for class template partial
specialization '__is_invocable_impl<_Result, _Ret, __void_t >' [with _Result = std::__invoke_result<(lambda at
:21:17) &&, const int &>, _Ret = void]

: __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, void>::type

  ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2696:7:
note: in instantiation of template class
'std::__is_invocable_impl:21:17) &&,
const int &>, void, void>' requested here

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2732:44:
note: in instantiation of template class 'std::is_invocable<(lambda at
:21:17) &&, const int &>' requested here

inline constexpr bool is_invocable_v = is_invocable<_Fn, _Args...>::value;

   ^

:13:27: note: in instantiation of variable template specialization
'std::is_invocable_v<(lambda at :21:17) &&, const int &>' requested
here

std::enable_if_t>

  ^

:21:14: note: while substituting deduced template arguments into
function template 'fn' [with F = (lambda at :21:17)]

instance.fn([](auto& i){

 ^

:21:26: note: variable 'i' declared const here

instance.fn([](auto& i){

   ~~^

1 error generated.

ASM generation compiler returned: 1

:24:11: error: cannot assign to variable 'i' with const-qualified type
'const int &'

i = 0; 

~ ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2283:7:
note: in instantiation of function template specialization 'main()::(anonymous
class)::operator()' requested here

  std::declval<_Fn>()(std::declval<_Args>()...)

  ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:2294:24:
note: while substituting de

[Bug c++/92270] Non-const ref treated as const-ref in the compiler.

2019-10-29 Thread jostaberry.jam at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92270

Yuki Wada  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |FIXED

--- Comment #1 from Yuki Wada  ---
I'm really sorry, this is a bug in my code.

The code above causes hard error by deducing lambda return type.
I fixed my code by giving the lambda an explicit return type rather than a
deduced return type:

instance.fn([](auto& i) -> void {
i = 0;
});

[Bug c++/90393] New: [9 Regression] ICE in return statement with a conditional operator, one of the second and third arguments is throw, and the other is a const variable of a class with a nontrivial

2019-05-08 Thread jostaberry.jam at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90393

Bug ID: 90393
   Summary: [9 Regression] ICE in return statement with a
conditional operator, one of the second and third
arguments is throw, and the other is a const variable
of a class with a nontrivial copy constructor.
   Product: gcc
   Version: 9.1.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: jostaberry.jam at gmail dot com
  Target Milestone: ---

This ICE occurred if the return statement is a conditional operator, one of the
second and third operands is throw, and the other is a const variable of a
class with a non-trivial copy constructor.

=== code: prog.cc ===
struct S {
S() = default;
S(const S&) {}
};

S f() {
const S m;
return true ? m : throw 0;
}

int main() {}
=== end code ===

=== command 
$ g++ ./prog.cpp
=== end command 

=== compiler output ===
prog.cc: In function 'S f()':
prog.cc:8:29: internal compiler error: in ocp_convert, at cp/cvt.c:766
8 | return true ? m : throw 0;
  | ^
0x588de1 ocp_convert(tree_node*, tree_node*, int, int, int)
../../gcc-9.1.0/gcc/cp/cvt.c:766
0x7195b2 check_return_expr(tree_node*, bool*)
../../gcc-9.1.0/gcc/cp/typeck.c:9799
0x6e87ff finish_return_stmt(tree_node*)
../../gcc-9.1.0/gcc/cp/semantics.c:894
0x68dd2d cp_parser_jump_statement
../../gcc-9.1.0/gcc/cp/parser.c:12919
0x68dd2d cp_parser_statement
../../gcc-9.1.0/gcc/cp/parser.c:11191
0x68e398 cp_parser_statement_seq_opt
../../gcc-9.1.0/gcc/cp/parser.c:11657
0x68e450 cp_parser_compound_statement
../../gcc-9.1.0/gcc/cp/parser.c:11611
0x6a49d0 cp_parser_function_body
../../gcc-9.1.0/gcc/cp/parser.c:22660
0x6a49d0 cp_parser_ctor_initializer_opt_and_function_body
../../gcc-9.1.0/gcc/cp/parser.c:22711
0x6a51b1 cp_parser_function_definition_after_declarator
../../gcc-9.1.0/gcc/cp/parser.c:27802
0x6a5e7b cp_parser_function_definition_from_specifiers_and_declarator
../../gcc-9.1.0/gcc/cp/parser.c:27718
0x6a5e7b cp_parser_init_declarator
../../gcc-9.1.0/gcc/cp/parser.c:20297
0x68b02e cp_parser_simple_declaration
../../gcc-9.1.0/gcc/cp/parser.c:13541
0x6ab1a0 cp_parser_declaration
../../gcc-9.1.0/gcc/cp/parser.c:13238
0x6ab8c9 cp_parser_translation_unit
../../gcc-9.1.0/gcc/cp/parser.c:4701
0x6ab8c9 c_parse_file()
../../gcc-9.1.0/gcc/cp/parser.c:41181
0x76605b c_common_parse_file()
../../gcc-9.1.0/gcc/c-family/c-opts.c:1156
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
=== end compiler output ===