[Bug c++/85958] Make const qualifier error clear

2020-05-06 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #12 from Jonny Grant  ---
Another example const.cpp attached.

The message doesn't mention it's the const qualifier.

Expected:
:6:21: error: passing 'const
std::vector >' as 'this' argument discards
const qualifier [-fpermissive]

6 | vec.push_back("");

  | ^



Output:


#1 with x86-64 gcc (trunk)
: In function 'void f(const
std::vector >&)':

:6:21: error: passing 'const
std::vector >' as 'this' argument discards
qualifiers [-fpermissive]

6 | vec.push_back("");

  | ^

In file included from
/opt/compiler-explorer/gcc-trunk-20200506/include/c++/11.0.0/vector:67,

 from :2:

/opt/compiler-explorer/gcc-trunk-20200506/include/c++/11.0.0/bits/stl_vector.h:1203:7:
note:   in call to 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp,
_Alloc>::value_type&&) [with _Tp = std::__cxx11::basic_string; _Alloc =
std::allocator >; std::vector<_Tp,
_Alloc>::value_type = std::__cxx11::basic_string]'

 1203 |   push_back(value_type&& __x)

  |   ^

Compiler returned: 1

[Bug c++/85958] Make const qualifier error clear

2020-05-06 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #11 from Jonny Grant  ---
Created attachment 48463
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48463&action=edit
argument discards qualifiers

Another example  "argument discards qualifiers"

[Bug c++/85958] Make const qualifier error clear

2018-06-06 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #10 from Jonny Grant  ---
(In reply to Jonathan Wakely from comment #7)
> (In reply to Jonny Grant from comment #5)
> > I personally feel "bind" is not a word any programming course teaches, we
> > say "passing parameters" or "passing arguments".
> 
> You pass arguments, which initialize parameters. Initialization of
> references is called binding.

Our C++ lectures always said "pass by reference". A quick search online seems
to say similar. Although I appreciate compiler developers will always use more
formally accurate terms.

https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value


> > 
> > In addition, I feel we don't think we really need the word "reference"
> 
> If the parameter type wasn't a reference there would be no problem. Omitting
> the reason it fails seems unhelpful.

My reasoning for not listing the word "reference" is that "int&" includes the
symbol & which means reference.   But a little duplication is ok.. only one
word.

> > Therefore, I suggest the following:
> > 
> > $ g++ -o main main.cpp -Wall -Werror -Wconversion
> > main.cpp: In function ‘int main()’:
> > main.cpp:11:25: error: cannot pass ‘const int’ to non-const ‘int&’
> 
> No this is nonsense. You are not passing something to a reference, you are
> passing it to the function. The object cannot be bound to the reference
> because of the cv-qualifiers.

Ok, point taken you are right.

So this I suggest:
 main.cpp:11:25: error: cannot pass ‘const int’ by non-const ‘int&’

Or with the "reference" word:
 main.cpp:11:25: error: cannot pass ‘const int’ by non-const ‘int&’ reference.

Although i do like W E Brown suggestion, I took out the "type":

"can't initialize parameter reference 'int&' with argument 'const int'"

> I'm keen to make the language clearer, but not by making it simply wrong
> about what's happening!

great! :)

[Bug c++/85958] Make const qualifier error clear

2018-06-04 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #9 from Jonathan Wakely  ---
(In reply to W E Brown from comment #8)
> (2) For the same reason, the note accompanying the diagnostic might at the
> same time be more accurately rephrased as "initializing parameter 1 of ..."
> instead of the current "initializing argument 1 of ...".

Agreed.

> (3) Finally, there seems to be a fair amount of redundancy in the note,
> mentioning the called function both with and without its parameter's name. 
> Perhaps just one of these might suffice?

The first occurrence is the function type (including any template arguments),
and is always shown. The second is the verbatim line from the source code
highlighted with carets, but is only shown conditionally (caret diagnostics can
be disabled with -fno-diagnostics-show-caret) and might not show the entire
function declaration. For example:

template
void f(int, 
   T&)  
{ } 

int main()  
{   
  f(1, 1);
}


f.cc: In function ‘int main()’:
f.cc:8:9: error: cannot bind non-const lvalue reference of type ‘int&’ to an
rvalue of type ‘int’
   f(1, 1);
 ^
f.cc:2:6: note:   initializing argument 2 of ‘void f(int, T&) [with T = int]’
 void f(int,
  ^

Removing the note here would mean losing valuable information (the caret
diagnostic doesn't show the right line, which is a separate bug, but even with
that fixed would only show one line not the whole function declaration).

[Bug c++/85958] Make const qualifier error clear

2018-06-03 Thread webrown.cpp at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #8 from W E Brown  ---
C++ seems very clear that the semantics of parameter passage are those of
initialization.  For example, according to [expr.call]/7 in N4741:

"When a function is called, each parameter shall be initialized with its
corresponding argument" [cross-references omitted].

(1) We might therefore consider to rephrase the diagnostic so as to be
consistent with the Standard's nomenclature.  For example:

"can't initialize parameter of type 'int&' with argument of type 'const int'"

Or, flipping the phrasing:

"argument of type 'const int' can't initialize parameter of type 'int&'"

(I prefer the latter, actually, because the caret indicates the argument.
YMMV.)

(2) For the same reason, the note accompanying the diagnostic might at the same
time be more accurately rephrased as "initializing parameter 1 of ..." instead
of the current "initializing argument 1 of ...".

(3) Finally, there seems to be a fair amount of redundancy in the note,
mentioning the called function both with and without its parameter's name. 
Perhaps just one of these might suffice?

Just some thoughts for your consideration.

[Bug c++/85958] Make const qualifier error clear

2018-06-03 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #7 from Jonathan Wakely  ---
(In reply to Jonny Grant from comment #5)
> I personally feel "bind" is not a word any programming course teaches, we
> say "passing parameters" or "passing arguments".

You pass arguments, which initialize parameters. Initialization of references
is called binding.


> 
> In addition, I feel we don't think we really need the word "reference"

If the parameter type wasn't a reference there would be no problem. Omitting
the reason it fails seems unhelpful.


> Therefore, I suggest the following:
> 
> $ g++ -o main main.cpp -Wall -Werror -Wconversion
> main.cpp: In function ‘int main()’:
> main.cpp:11:25: error: cannot pass ‘const int’ to non-const ‘int&’

No this is nonsense. You are not passing something to a reference, you are
passing it to the function. The object cannot be bound to the reference because
of the cv-qualifiers.

I'm keen to make the language clearer, but not by making it simply wrong about
what's happening!

[Bug c++/85958] Make const qualifier error clear

2018-06-01 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #6 from Jonny Grant  ---
Clang shows:

$ clang -o main main.cpp -Wall -Werror -Wconversion
main.cpp:15:5: error: no matching function for call to 'strstripspace'
strstripspace(unused, two);
^
main.cpp:5:6: note: candidate function not viable: 1st argument ('const int')
  would lose const qualifier
void strstripspace(int & value, int & two)
 ^
1 error generated.

[Bug c++/85958] Make const qualifier error clear

2018-06-01 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #5 from Jonny Grant  ---
(In reply to Tavian Barnes from comment #4)
> IMHO "discards qualifiers" and even "discards const qualifier" are still
> confusing.  Making it clearly counterfactual, as in "...would discard
> (const) qualifier(s)...," would be an improvement.
> 
> But I'd further argue that "discarding qualifiers" is not really how most
> people think of this kind of error.  When a minor tries to get into a bar,
> they are not told that "entering this bar discards your age," they are told
> that "minors aren't allowed."  So I think "cannot bind 'const int' to
> non-const reference type 'int&'" would be more intuitive phrasing.

Hi Tavian

Yes, your proposal sounds good. Definitely clearer English would be very
helpful.

I personally feel "bind" is not a word any programming course teaches, we say
"passing parameters" or "passing arguments".

In addition, I feel we don't think we really need the word "reference"

Therefore, I suggest the following:

$ g++ -o main main.cpp -Wall -Werror -Wconversion
main.cpp: In function ‘int main()’:
main.cpp:11:25: error: cannot pass ‘const int’ to non-const ‘int&’

[Bug c++/85958] Make const qualifier error clear

2018-05-30 Thread tavianator at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

Tavian Barnes  changed:

   What|Removed |Added

 CC||tavianator at gmail dot com

--- Comment #4 from Tavian Barnes  ---
IMHO "discards qualifiers" and even "discards const qualifier" are still
confusing.  Making it clearly counterfactual, as in "...would discard (const)
qualifier(s)...," would be an improvement.

But I'd further argue that "discarding qualifiers" is not really how most
people think of this kind of error.  When a minor tries to get into a bar, they
are not told that "entering this bar discards your age," they are told that
"minors aren't allowed."  So I think "cannot bind 'const int' to non-const
reference type 'int&'" would be more intuitive phrasing.

[Bug c++/85958] Make const qualifier error clear

2018-05-30 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #3 from Jonny Grant  ---
(In reply to Jonathan Wakely from comment #2)
> (In reply to Jonny Grant from comment #1)
> > My idea of what would be clearest is :-
> > 
> > Cannot pass ‘const int‘ as ‘int&‘ (non const)
> 
> I don't think that's an improvement at all.
> 
> But "... discards const qualifier" or "... discards volatile qualifier" or
> "...discards const and volatile qualifiers" might be OK. Or saying "as
> reference of non-const type 'int&'" similar to my proposed fix for PR 53281.

Sounds good. I support this improvement!

[Bug c++/85958] Make const qualifier error clear

2018-05-29 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

Jonathan Wakely  changed:

   What|Removed |Added

   See Also||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=53281

--- Comment #2 from Jonathan Wakely  ---
(In reply to Jonny Grant from comment #1)
> My idea of what would be clearest is :-
> 
> Cannot pass ‘const int‘ as ‘int&‘ (non const)

I don't think that's an improvement at all.

But "... discards const qualifier" or "... discards volatile qualifier" or
"...discards const and volatile qualifiers" might be OK. Or saying "as
reference of non-const type 'int&'" similar to my proposed fix for PR 53281.

[Bug c++/85958] Make const qualifier error clear

2018-05-29 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85958

--- Comment #1 from Jonny Grant  ---
My idea of what would be clearest is :-

Cannot pass ‘const int‘ as ‘int&‘ (non const)