Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest using slice when encountering let x = ""[..]; #46249

Merged
merged 4 commits into from
Nov 28, 2017

Conversation

estebank
Copy link
Contributor

Fix #26319.

@rust-highfive
Copy link
Collaborator

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@eddyb
Copy link
Member

eddyb commented Nov 24, 2017

I don't think the index type matters, we should just not check it and just emit the suggestion.

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 25, 2017

fn main() {
let s = "abc";
let t = if true { s[..2] } else { s };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add //~ ERROR comments. Ui tests work like compile-fail now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -821,6 +823,33 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
err.emit();
}

/// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just for assignments and not all the cases in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other cases in the test go through type check instead. I'll see if I can expand #46256 to also suggest in these cases. I included all the cases originally mentioned in order to make sure we don't regress in any of the cases (without noticing).

@estebank estebank force-pushed the suggest-slice branch 3 times, most recently from 134889a to 3b56558 Compare November 25, 2017 14:50
@estebank
Copy link
Contributor Author

@eddyb I believe you were right so removed the index type check.

@eddyb
Copy link
Member

eddyb commented Nov 25, 2017

r? @nikomatsakis I still feel this is much more specific than it should be, but I don't have any good suggestions, other than perhaps that & isn't called "slice-ing" or "taking a slice".
a[..] does the slicing, & just borrows that slice. You can also have a[..].slice_method().

In general, both deref and indexing could use a suggestion to borrow the resulting lvalue.

@rust-highfive rust-highfive assigned nikomatsakis and unassigned eddyb Nov 25, 2017
--> $DIR/str-array-assignment.rs:17:27
|
11 | fn main() { //~ NOTE expected `()` because of default return type
| - expected `()` because of default return type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks dubious. Could you look into this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I noticed that too. Still digging.

Copy link
Contributor

@arielb1 arielb1 Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's also already the case on nightly. Feel free to ignore this and open an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arielb1
Copy link
Contributor

arielb1 commented Nov 27, 2017

This feels hacky to me, but I don't see any better way of implementing this (the rhs of locals is an lvalue, so checks based on it being an rvalue won't quite work).

btw, you should be checking that the problem type is a slice or string - if the return type is a struct or trait object, you don't want the error message to mention "slices".

@arielb1
Copy link
Contributor

arielb1 commented Nov 27, 2017

OTOH, having an Index implementation that returns trait objects is a bit weird - that would mean someone does some kind of indirection somewhere. But I think "consider borrowing here" would look better than "consider borrowing the slice" (also because str is not a "slice").

@arielb1 arielb1 closed this Nov 27, 2017
@arielb1 arielb1 reopened this Nov 27, 2017
@estebank
Copy link
Contributor Author

@arielb1 reworded.

@arielb1
Copy link
Contributor

arielb1 commented Nov 27, 2017

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 27, 2017

📌 Commit fa44927 has been approved by arielb1

kennytm added a commit to kennytm/rust that referenced this pull request Nov 27, 2017
Suggest using slice when encountering `let x = ""[..];`

Fix rust-lang#26319.
bors added a commit that referenced this pull request Nov 27, 2017
Rollup of 10 pull requests

- Successful merges: #45506, #46174, #46231, #46240, #46249, #46258, #46262, #46275, #46282, #46285
- Failed merges:
@bors bors merged commit fa44927 into rust-lang:master Nov 28, 2017
@estebank estebank deleted the suggest-slice branch November 9, 2023 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants