-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Improve diagnostics when closure doesn't meet trait bound #80635
Merged
bors
merged 1 commit into
rust-lang:master
from
sexxi-goose:use-place-instead-of-symbol
Jan 17, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,9 @@ pub use self::IntVarValue::*; | |
pub use self::Variance::*; | ||
|
||
use crate::hir::exports::ExportMap; | ||
use crate::hir::place::Place as HirPlace; | ||
use crate::hir::place::{ | ||
Place as HirPlace, PlaceBase as HirPlaceBase, ProjectionKind as HirProjectionKind, | ||
}; | ||
use crate::ich::StableHashingContext; | ||
use crate::middle::cstore::CrateStoreDyn; | ||
use crate::middle::resolve_lifetime::ObjectLifetimeDefault; | ||
|
@@ -734,6 +736,43 @@ pub struct CapturedPlace<'tcx> { | |
pub info: CaptureInfo<'tcx>, | ||
} | ||
|
||
pub fn place_to_string_for_capture(tcx: TyCtxt<'tcx>, place: &HirPlace<'tcx>) -> String { | ||
let name = match place.base { | ||
HirPlaceBase::Upvar(upvar_id) => tcx.hir().name(upvar_id.var_path.hir_id).to_string(), | ||
_ => bug!("Capture_information should only contain upvars"), | ||
}; | ||
let mut curr_string = name; | ||
|
||
for (i, proj) in place.projections.iter().enumerate() { | ||
match proj.kind { | ||
HirProjectionKind::Deref => { | ||
curr_string = format!("*{}", curr_string); | ||
} | ||
HirProjectionKind::Field(idx, variant) => match place.ty_before_projection(i).kind() { | ||
ty::Adt(def, ..) => { | ||
curr_string = format!( | ||
"{}.{}", | ||
curr_string, | ||
def.variants[variant].fields[idx as usize].ident.name.as_str() | ||
); | ||
} | ||
ty::Tuple(_) => { | ||
curr_string = format!("{}.{}", curr_string, idx); | ||
} | ||
_ => { | ||
bug!( | ||
"Field projection applied to a type other than Adt or Tuple: {:?}.", | ||
place.ty_before_projection(i).kind() | ||
) | ||
} | ||
}, | ||
proj => bug!("{:?} unexpected because it isn't captured", proj), | ||
} | ||
} | ||
|
||
curr_string.to_string() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why convert String back to String? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not needed, not sure why I did it 😅 |
||
} | ||
|
||
/// Part of `MinCaptureInformationMap`; describes the capture kind (&, &mut, move) | ||
/// for a particular capture as well as identifying the part of the source code | ||
/// that triggered this capture to occur. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ui/closures/2229_closure_analysis/diagnostics/closure-origin-multi-variant-diagnostics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![feature(capture_disjoint_fields)] | ||
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete | ||
//~| `#[warn(incomplete_features)]` on by default | ||
//~| see issue #53488 <https://github.com/rust-lang/rust/issues/53488> | ||
|
||
// Check that precise paths are being reported back in the error message. | ||
|
||
|
||
enum MultiVariant { | ||
Point(i32, i32), | ||
Meta(i32) | ||
} | ||
|
||
fn main() { | ||
let mut point = MultiVariant::Point(10, -10,); | ||
|
||
let mut meta = MultiVariant::Meta(1); | ||
|
||
let c = || { | ||
if let MultiVariant::Point(ref mut x, _) = point { | ||
*x += 1; | ||
} | ||
|
||
if let MultiVariant::Meta(ref mut v) = meta { | ||
*v += 1; | ||
} | ||
}; | ||
|
||
let a = c; | ||
let b = c; //~ ERROR use of moved value: `c` [E0382] | ||
} |
26 changes: 26 additions & 0 deletions
26
...losures/2229_closure_analysis/diagnostics/closure-origin-multi-variant-diagnostics.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes | ||
--> $DIR/closure-origin-multi-variant-diagnostics.rs:1:12 | ||
| | ||
LL | #![feature(capture_disjoint_fields)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information | ||
|
||
error[E0382]: use of moved value: `c` | ||
--> $DIR/closure-origin-multi-variant-diagnostics.rs:30:13 | ||
| | ||
LL | let a = c; | ||
| - value moved here | ||
LL | let b = c; | ||
| ^ value used here after move | ||
| | ||
note: closure cannot be moved more than once as it is not `Copy` due to moving the variable `point.0` out of its environment | ||
--> $DIR/closure-origin-multi-variant-diagnostics.rs:20:52 | ||
| | ||
LL | if let MultiVariant::Point(ref mut x, _) = point { | ||
| ^^^^^ | ||
|
||
error: aborting due to previous error; 1 warning emitted | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
26 changes: 26 additions & 0 deletions
26
...i/closures/2229_closure_analysis/diagnostics/closure-origin-single-variant-diagnostics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![feature(capture_disjoint_fields)] | ||
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete | ||
//~| `#[warn(incomplete_features)]` on by default | ||
//~| see issue #53488 <https://github.com/rust-lang/rust/issues/53488> | ||
|
||
// Check that precise paths are being reported back in the error message. | ||
|
||
enum SingleVariant { | ||
Point(i32, i32), | ||
} | ||
|
||
fn main() { | ||
let mut point = SingleVariant::Point(10, -10); | ||
|
||
let c = || { | ||
// FIXME(project-rfc-2229#24): Change this to be a destructure pattern | ||
// once this is fixed, to remove the warning. | ||
if let SingleVariant::Point(ref mut x, _) = point { | ||
//~^ WARNING: irrefutable if-let pattern | ||
*x += 1; | ||
} | ||
}; | ||
|
||
let b = c; | ||
let a = c; //~ ERROR use of moved value: `c` [E0382] | ||
} |
37 changes: 37 additions & 0 deletions
37
...osures/2229_closure_analysis/diagnostics/closure-origin-single-variant-diagnostics.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes | ||
--> $DIR/closure-origin-single-variant-diagnostics.rs:1:12 | ||
| | ||
LL | #![feature(capture_disjoint_fields)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information | ||
|
||
warning: irrefutable if-let pattern | ||
--> $DIR/closure-origin-single-variant-diagnostics.rs:18:9 | ||
| | ||
LL | / if let SingleVariant::Point(ref mut x, _) = point { | ||
LL | | | ||
LL | | *x += 1; | ||
LL | | } | ||
| |_________^ | ||
| | ||
= note: `#[warn(irrefutable_let_patterns)]` on by default | ||
|
||
error[E0382]: use of moved value: `c` | ||
--> $DIR/closure-origin-single-variant-diagnostics.rs:25:13 | ||
| | ||
LL | let b = c; | ||
| - value moved here | ||
LL | let a = c; | ||
| ^ value used here after move | ||
| | ||
note: closure cannot be moved more than once as it is not `Copy` due to moving the variable `point.0` out of its environment | ||
--> $DIR/closure-origin-single-variant-diagnostics.rs:18:53 | ||
| | ||
LL | if let SingleVariant::Point(ref mut x, _) = point { | ||
| ^^^^^ | ||
|
||
error: aborting due to previous error; 2 warnings emitted | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
25 changes: 25 additions & 0 deletions
25
src/test/ui/closures/2229_closure_analysis/diagnostics/closure-origin-struct-diagnostics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![feature(capture_disjoint_fields)] | ||
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete | ||
//~| `#[warn(incomplete_features)]` on by default | ||
//~| see issue #53488 <https://github.com/rust-lang/rust/issues/53488> | ||
|
||
// Check that precise paths are being reported back in the error message. | ||
|
||
struct Y { | ||
y: X | ||
} | ||
|
||
struct X { | ||
a: u32, | ||
b: u32, | ||
} | ||
|
||
fn main() { | ||
let mut x = Y { y: X { a: 5, b: 0 } }; | ||
let hello = || { | ||
x.y.a += 1; | ||
}; | ||
|
||
let b = hello; | ||
let c = hello; //~ ERROR use of moved value: `hello` [E0382] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: you can use
write!
macro to append to strings more efficiently -- here is an example