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

Give better suggestion when const Range*'s are used as patterns #76222

Merged
merged 1 commit into from
Sep 12, 2020

Conversation

guswynn
Copy link
Contributor

@guswynn guswynn commented Sep 1, 2020

Fixes #76191

let me know if there is more/different information you want to show in this case

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 1, 2020
src/test/ui/issues/issue-76191.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
@guswynn guswynn changed the title Link to tracking issue when const's are used as patterns Give better suggest when const Range*'s are used as patterns Sep 3, 2020
@guswynn guswynn changed the title Give better suggest when const Range*'s are used as patterns Give better suggestion when const Range*'s are used as patterns Sep 3, 2020
compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
Comment on lines 1 to 18
error[E0308]: mismatched types
--> $DIR/issue-76191.rs:10:9
|
LL | const RANGE: RangeInclusive<i32> = 0..=255;
| ------------------------------------------- constant defined here
...
LL | match n {
| - this expression has type `i32`
LL | RANGE => {}
| ^^^^^
| |
| expected `i32`, found struct `std::ops::RangeInclusive`
| `RANGE` is interpreted as a constant, not a new binding
| help: introduce a new binding instead: `other_range`
|
= note: expected type `i32`
found struct `std::ops::RangeInclusive<i32>`
= note: Constants only support matching the same type. If you are using range syntax (i.e. ..= or similar) in your constant you may want to move the range into the match block
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally the output would look closer to the following:

error[E0308]: mismatched types
  --> $DIR/issue-76191.rs:10:9
   |
LL | const RANGE: RangeInclusive<i32> = 0..=255;
   | ------------------------------------------- constant defined here
...
LL |     match n {
   |           - this expression has type `i32`
LL |         RANGE => {}
   |         ^^^^^
   |         |
   |         expected `i32`, found struct `std::ops::RangeInclusive`
   |         `RANGE` is interpreted as a constant, not a new binding
   |
   = note: expected type `i32`
            found struct `std::ops::RangeInclusive<i32>`
   = note: constants only support matching the same type
help: you may want to move the range into the match block
   |
LL |         0..=255 => {}
   |         ^^^^^^^

The later suggestion might not be feasible (depending on whether you can get the hir::Expr corresponding to the const).

Copy link
Contributor

Choose a reason for hiding this comment

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

(This is not a blocker).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh hmm, I can look into doing that, but would likely make that a separate pull request

src/test/ui/issues/issue-76191.stderr Outdated Show resolved Hide resolved
src/test/ui/issues/issue-76191.stderr Outdated Show resolved Hide resolved
@guswynn
Copy link
Contributor Author

guswynn commented Sep 3, 2020

Updated to take into account @estebank's comments, as well as only emit this new error when its const as well as a range, as well as (try) to fix the fmt ci failure

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 8, 2020
@guswynn
Copy link
Contributor Author

guswynn commented Sep 11, 2020

Do I need to squash these commits?

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

Can you squash your commits? r=me after that.

Comment on lines 790 to 792
let msg = "constants only support matching by type, \
if you meant to match against a range of values, \
consider using a range pattern like `min ..= max` in the match block";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let msg = "constants only support matching by type, \
if you meant to match against a range of values, \
consider using a range pattern like `min ..= max` in the match block";
let msg = "constants only support matching by type, \
if you meant to match against a range of values, \
consider using a range pattern like `min ..= max` in the match block";

e.span_suggestion(ident.span, msg, sugg, Applicability::HasPlaceholders);
let const_def_id = match pat_ty.kind {
Adt(def, _) => match res {
Res::Def(DefKind::Const, _) => Some(def.did),
Copy link
Contributor

Choose a reason for hiding this comment

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

If we take the DefId from the Res...

let msg = "constants only support matching by type, \
if you meant to match against a range of values, \
consider using a range pattern like `min ..= max` in the match block";
e.note(msg);
Copy link
Contributor

Choose a reason for hiding this comment

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

...we can use that DefId here to do something like the following so that we could provide a structured suggestion

                         match self.tcx.hir().get_if_local(def_id)
                            hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_ty, body_id), ..}) => {
                                // use the body_id to get the `const`'s init pattern
                            }
                            _ => {

                            }
                        }

But I don't think this needs to be in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ill make a personal note to make a followup, as I'm still learning all this stuff its nice to do things 1 bit at a time

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me

@guswynn
Copy link
Contributor Author

guswynn commented Sep 11, 2020

bitten by: #76222, one sec!

@guswynn guswynn force-pushed the const_diag branch 2 times, most recently from a690475 to 9cdbac3 Compare September 11, 2020 22:01
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 11, 2020

📌 Commit 5e126c9 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 11, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 12, 2020
Give better suggestion when const Range*'s are used as patterns

Fixes rust-lang#76191

let me know if there is more/different information you want to show in this case
@bors
Copy link
Contributor

bors commented Sep 12, 2020

⌛ Testing commit 5e126c9 with merge 2e2e7de...

@bors
Copy link
Contributor

bors commented Sep 12, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: estebank
Pushing 2e2e7de to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 12, 2020
@bors bors merged commit 2e2e7de into rust-lang:master Sep 12, 2020
@rustbot rustbot added this to the 1.48.0 milestone Sep 12, 2020
@guswynn guswynn deleted the const_diag branch September 15, 2020 15:27
RalfJung added a commit to RalfJung/rust that referenced this pull request Sep 19, 2020
give *even better* suggestion when matching a const range

notice that the err already has "constant defined here"
so this is now *exceedingly clear*

extension to rust-lang#76222

r? @estebank
RalfJung added a commit to RalfJung/rust that referenced this pull request Sep 19, 2020
give *even better* suggestion when matching a const range

notice that the err already has "constant defined here"
so this is now *exceedingly clear*

extension to rust-lang#76222

r? @estebank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Matching a const Range
8 participants