-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Description
UPDATE: If you are interested in fixing this bug, there are mentoring instructions in this comment below.
fn main() {
let x = plus_one(5);
println!("X = {}", x);
}
fn plus_one(x: i32) -> i32 {
x + 1;
}
|
6 | fn plus_one(x: i32) -> i32 {
| ____________________________^ starting here...
7 | | x + 1;
8 | | }
| |_^ ...ending here: expected (), found i32
|
= note: expected type `()`
found type `i32`
I think that types in "expected type ()
found type i32
" needs to be exchanged
$ rustc --version
rustc 1.18.0-nightly (452bf0852 2017-04-19)
Metadata
Metadata
Assignees
Labels
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
estebank commentedon Apr 22, 2017
I believe this was introduced by #40224, specifically this line. Swapping the items in that tuple reflects the expected output:
CC @nikomatsakis
nikomatsakis commentedon Apr 24, 2017
Hmm, yes. Well, you certainly can't change just that one line, because it is correct in most other cases. The flawed assumption here is that the "forced unit" case means that
()
should always be the "expected" type. I guess we'll have to thread a parameter through. This seems like a good mentoring issue, so I'm going to write up some instructions:@estebank correctly identified the problem here. The coercion code includes a function called
coerce_force_unit()
covering cases where the rust syntax itself supplies a type of()
; examples includereturn;
(no argument expression; only legal if the return type is unit);{foo;}
(no tail expression); the type of awhile
loop; orbreak;
(no argument expression). I incorrectly assumed we would always want to label the()
type in such cases as the "expected type", but this example provides a counterexample.To account for this, I think we need to add a parameter to
coerce_force_unit()
, defined here. I would called it something likelabel_unit_as_expected: bool
. This will then have to be passed tocoerce_inner()
, which can have a new argument likelabel_expression_as_expected: bool
.coerce_inner()
is also called fromcoerce()
; in that case, I think we can just passfalse
for this argument.Naturally, we will need to change the callers to
coerce_forced_unit
to add the new argument. Most of them can usetrue
(and thus keep the current behavior), but this one call is where we type-check blocks. In that case, we can passfalse
, I think. It'd probably be good to add a comment citing this issue, something like:Meanwhile, when an error occurs, we will not check if
expression.is_none()
, but rather we'll check forlabel_expression_as_expected
being true. This comment and assertion can be removed since they no longer seem appropriate.After that, we just want to add a
ui
test for this case to prevent it from regressing again. I'd call it something likesrc/test/ui/coercion-missing-tail-expected-type.rs
and include a comment referencing this issue number.Tagged as E-mentor. If you're interested, please leave a comment to let others know you are working on it! Feel free to ask questions here or to find me on IRC (
nmatsakis
) or Gitter (@nikomatsakis
).alexeyzab commentedon Apr 24, 2017
Hi there! I'd like to try implementing this.
nikomatsakis commentedon Apr 25, 2017
@alexeyzab great! let me know how it goes =)
Fix error message for mismatched types
alexeyzab commentedon Apr 25, 2017
@nikomatsakis I've added the changes you mentioned, seems to work correctly. Let me know if I should change anything in my PR. Thanks for explaining everything! :)
Auto merge of #41547 - alexeyzab:41425-fix-mismatched-types-error-mes…
shioju commentedon May 3, 2017
Looks like this issue has been resolved. Can someone close this please, so that people looking for issues to work on can skip this. Thanks.