-
Notifications
You must be signed in to change notification settings - Fork 13.3k
pretty.rs: Update Closure and Generator print #77069
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
---|---|---|
|
@@ -641,104 +641,86 @@ pub trait PrettyPrinter<'tcx>: | |
} | ||
ty::Str => p!(write("str")), | ||
ty::Generator(did, substs, movability) => { | ||
p!(write("[")); | ||
match movability { | ||
hir::Movability::Movable => p!(write("[generator")), | ||
hir::Movability::Static => p!(write("[static generator")), | ||
hir::Movability::Movable => {} | ||
hir::Movability::Static => p!(write("static ")), | ||
} | ||
|
||
// FIXME(eddyb) should use `def_span`. | ||
if let Some(did) = did.as_local() { | ||
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); | ||
let span = self.tcx().hir().span(hir_id); | ||
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span))); | ||
|
||
if substs.as_generator().is_valid() { | ||
let upvar_tys = substs.as_generator().upvar_tys(); | ||
let mut sep = " "; | ||
for (&var_id, upvar_ty) in self | ||
.tcx() | ||
.upvars_mentioned(did) | ||
.as_ref() | ||
.iter() | ||
.flat_map(|v| v.keys()) | ||
.zip(upvar_tys) | ||
{ | ||
p!(write("{}{}:", sep, self.tcx().hir().name(var_id)), print(upvar_ty)); | ||
sep = ", "; | ||
} | ||
if !self.tcx().sess.verbose() { | ||
p!(write("generator")); | ||
// FIXME(eddyb) should use `def_span`. | ||
if let Some(did) = did.as_local() { | ||
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); | ||
let span = self.tcx().hir().span(hir_id); | ||
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span))); | ||
} else { | ||
p!(write("@{}", self.tcx().def_path_str(did))); | ||
} | ||
} else { | ||
p!(write("@{}", self.tcx().def_path_str(did))); | ||
|
||
p!(print_def_path(did, substs)); | ||
if substs.as_generator().is_valid() { | ||
let upvar_tys = substs.as_generator().upvar_tys(); | ||
let mut sep = " "; | ||
for (index, upvar_ty) in upvar_tys.enumerate() { | ||
p!(write("{}{}:", sep, index), print(upvar_ty)); | ||
sep = ", "; | ||
// Search for the first inference variable | ||
p!(write(" upvar_tys=(")); | ||
let mut uninferred_ty = | ||
substs.as_generator().upvar_tys().filter(|ty| ty.is_ty_infer()); | ||
if uninferred_ty.next().is_some() { | ||
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. Won't need this check when rust-lang/project-rfc-2229#4 is implemented, because then the is_valid() check itself will fail because the |
||
p!(write("unavailable")); | ||
} else { | ||
self = self.comma_sep(substs.as_generator().upvar_tys())?; | ||
} | ||
p!(write(")")); | ||
} | ||
} | ||
|
||
if substs.as_generator().is_valid() { | ||
p!(write(" "), print(substs.as_generator().witness())); | ||
} | ||
|
||
p!(write("]")) | ||
p!(write("]")); | ||
} | ||
ty::GeneratorWitness(types) => { | ||
p!(in_binder(&types)); | ||
} | ||
ty::Closure(did, substs) => { | ||
p!(write("[closure")); | ||
|
||
// FIXME(eddyb) should use `def_span`. | ||
if let Some(did) = did.as_local() { | ||
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); | ||
if self.tcx().sess.opts.debugging_opts.span_free_formats { | ||
p!(write("@"), print_def_path(did.to_def_id(), substs)); | ||
} else { | ||
let span = self.tcx().hir().span(hir_id); | ||
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span))); | ||
} | ||
|
||
if substs.as_closure().is_valid() { | ||
let upvar_tys = substs.as_closure().upvar_tys(); | ||
let mut sep = " "; | ||
for (&var_id, upvar_ty) in self | ||
.tcx() | ||
.upvars_mentioned(did) | ||
.as_ref() | ||
.iter() | ||
.flat_map(|v| v.keys()) | ||
.zip(upvar_tys) | ||
{ | ||
p!(write("{}{}:", sep, self.tcx().hir().name(var_id)), print(upvar_ty)); | ||
sep = ", "; | ||
p!(write("[")); | ||
if !self.tcx().sess.verbose() { | ||
davidtwco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
p!(write("closure")); | ||
// FIXME(eddyb) should use `def_span`. | ||
if let Some(did) = did.as_local() { | ||
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); | ||
if self.tcx().sess.opts.debugging_opts.span_free_formats { | ||
p!(write("@"), print_def_path(did.to_def_id(), substs)); | ||
} else { | ||
let span = self.tcx().hir().span(hir_id); | ||
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span))); | ||
} | ||
} else { | ||
p!(write("@{}", self.tcx().def_path_str(did))); | ||
} | ||
} else { | ||
p!(write("@{}", self.tcx().def_path_str(did))); | ||
|
||
p!(print_def_path(did, substs)); | ||
if substs.as_closure().is_valid() { | ||
let upvar_tys = substs.as_closure().upvar_tys(); | ||
let mut sep = " "; | ||
for (index, upvar_ty) in upvar_tys.enumerate() { | ||
p!(write("{}{}:", sep, index), print(upvar_ty)); | ||
sep = ", "; | ||
// Search for the first inference variable | ||
let mut uninferred_ty = | ||
substs.as_closure().upvar_tys().filter(|ty| ty.is_ty_infer()); | ||
if uninferred_ty.next().is_some() { | ||
// If the upvar substs contain an inference variable we haven't | ||
// finished capture analysis. | ||
p!(write(" closure_substs=(unavailable)")); | ||
} else { | ||
p!(write(" closure_kind_ty="), print(substs.as_closure().kind_ty())); | ||
p!( | ||
write(" closure_sig_as_fn_ptr_ty="), | ||
print(substs.as_closure().sig_as_fn_ptr_ty()) | ||
); | ||
p!(write(" upvar_tys=(")); | ||
self = self.comma_sep(substs.as_closure().upvar_tys())?; | ||
p!(write(")")); | ||
} | ||
} | ||
} | ||
|
||
if self.tcx().sess.verbose() && substs.as_closure().is_valid() { | ||
p!(write(" closure_kind_ty="), print(substs.as_closure().kind_ty())); | ||
p!( | ||
write(" closure_sig_as_fn_ptr_ty="), | ||
print(substs.as_closure().sig_as_fn_ptr_ty()) | ||
); | ||
} | ||
|
||
p!(write("]")) | ||
p!(write("]")); | ||
} | ||
ty::Array(ty, sz) => { | ||
p!(write("["), print(ty), write("; ")); | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
fn to_fn_once<F: FnOnce()>(f: F) -> F { | ||
f | ||
} | ||
|
||
fn f<T: std::fmt::Display>(y: T) { | ||
struct Foo<U: std::fmt::Display> { | ||
x: U, | ||
}; | ||
|
||
let foo = Foo { x: "x" }; | ||
|
||
let c = to_fn_once(move || { | ||
println!("{} {}", foo.x, y); | ||
}); | ||
|
||
c(); | ||
c(); | ||
//~^ ERROR use of moved value | ||
} | ||
|
||
fn main() { | ||
f("S"); | ||
} |
This file contains hidden or 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,20 @@ | ||
error[E0382]: use of moved value: `c` | ||
--> $DIR/closure-print-generic-1.rs:17:5 | ||
| | ||
LL | let c = to_fn_once(move || { | ||
| - move occurs because `c` has type `[closure@$DIR/closure-print-generic-1.rs:12:24: 14:6]`, which does not implement the `Copy` trait | ||
... | ||
LL | c(); | ||
| --- `c` moved due to this call | ||
LL | c(); | ||
| ^ value used here after move | ||
| | ||
note: this value implements `FnOnce`, which causes it to be moved when called | ||
--> $DIR/closure-print-generic-1.rs:16:5 | ||
| | ||
LL | c(); | ||
| ^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
This file contains hidden or 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,13 @@ | ||
mod mod1 { | ||
pub fn f<T: std::fmt::Display>(t: T) { | ||
let x = 20; | ||
|
||
let c = || println!("{} {}", t, x); | ||
let c1: () = c; | ||
//~^ ERROR mismatched types | ||
} | ||
} | ||
|
||
fn main() { | ||
mod1::f(5i32); | ||
} |
This file contains hidden or 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,20 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/closure-print-generic-2.rs:6:22 | ||
| | ||
LL | let c = || println!("{} {}", t, x); | ||
| -------------------------- the found closure | ||
LL | let c1: () = c; | ||
| -- ^ expected `()`, found closure | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected unit type `()` | ||
found closure `[closure@$DIR/closure-print-generic-2.rs:5:17: 5:43]` | ||
help: use parentheses to call this closure | ||
| | ||
LL | let c1: () = c(); | ||
| ^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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.
Out of curiosity, did you try using
def_span
here?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.
Didn't try def_span, I guess I could've. I was mostly trying to keep changes minimal to non-verbose mode with this PR. Main motivation is to remove printing upvar_tys for non-verbose.