-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Improve pretty printing of valtrees for references #98643
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 2 commits
Commits
Show all changes
4 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
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,84 @@ | ||
| use rustc_middle::ty::{self, TyCtxt}; | ||
| use rustc_target::abi::VariantIdx; | ||
|
|
||
| /// Tries to destructure constants of type Array or Adt into the constants | ||
| /// of its fields. | ||
voidc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
voidc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub(crate) fn destructure_const<'tcx>( | ||
| tcx: TyCtxt<'tcx>, | ||
| const_: ty::Const<'tcx>, | ||
| ) -> ty::DestructuredConst<'tcx> { | ||
| if let ty::ConstKind::Value(valtree) = const_.kind() { | ||
voidc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let branches = match valtree { | ||
| ty::ValTree::Branch(b) => b, | ||
| _ => bug!("cannot destructure constant {:?}", const_), | ||
| }; | ||
|
|
||
| let (fields, variant) = match const_.ty().kind() { | ||
| ty::Array(inner_ty, _) | ty::Slice(inner_ty) => { | ||
| // construct the consts for the elements of the array/slice | ||
| let field_consts = branches | ||
| .iter() | ||
| .map(|b| { | ||
| tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Value(*b), ty: *inner_ty }) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| debug!(?field_consts); | ||
|
|
||
| (field_consts, None) | ||
| } | ||
| ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"), | ||
| ty::Adt(def, substs) => { | ||
| let variant_idx = if def.is_enum() { | ||
| VariantIdx::from_u32(branches[0].unwrap_leaf().try_to_u32().unwrap()) | ||
voidc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| VariantIdx::from_u32(0) | ||
| }; | ||
| let fields = &def.variant(variant_idx).fields; | ||
| let mut field_consts = Vec::with_capacity(fields.len()); | ||
|
|
||
| // Note: First element inValTree corresponds to variant of enum | ||
| let mut valtree_idx = if def.is_enum() { 1 } else { 0 }; | ||
| for field in fields { | ||
| let field_ty = field.ty(tcx, substs); | ||
| let field_valtree = branches[valtree_idx]; // first element of branches is variant | ||
| let field_const = tcx.mk_const(ty::ConstS { | ||
| kind: ty::ConstKind::Value(field_valtree), | ||
| ty: field_ty, | ||
| }); | ||
| field_consts.push(field_const); | ||
| valtree_idx += 1; | ||
| } | ||
| debug!(?field_consts); | ||
|
|
||
| (field_consts, Some(variant_idx)) | ||
| } | ||
| ty::Tuple(elem_tys) => { | ||
| let fields = elem_tys | ||
voidc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .iter() | ||
| .enumerate() | ||
| .map(|(i, elem_ty)| { | ||
| let elem_valtree = branches[i]; | ||
| tcx.mk_const(ty::ConstS { | ||
| kind: ty::ConstKind::Value(elem_valtree), | ||
| ty: elem_ty, | ||
| }) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| (fields, None) | ||
| } | ||
| _ => bug!("cannot destructure constant {:?}", const_), | ||
| }; | ||
|
|
||
| let fields = tcx.arena.alloc_from_iter(fields.into_iter()); | ||
|
|
||
| ty::DestructuredConst { variant, fields } | ||
| } else { | ||
| bug!("cannot destructure constant {:?}", const_) | ||
| } | ||
| } | ||
|
|
||
| pub fn provide(providers: &mut ty::query::Providers) { | ||
| *providers = | ||
| ty::query::Providers { destructure_const, ..*providers }; | ||
| } | ||
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,28 @@ | ||
| #![feature(adt_const_params)] | ||
| #![allow(incomplete_features)] | ||
|
|
||
| #[derive(Debug, PartialEq, Eq)] | ||
| struct Foo { | ||
| value: i32, | ||
| nested: &'static Bar<i32>, | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq)] | ||
| struct Bar<T>(T); | ||
|
|
||
| struct Test<const F: Foo>; | ||
|
|
||
| fn main() { | ||
| let x: Test<{ | ||
| Foo { | ||
| value: 3, | ||
| nested: &Bar(4), | ||
| } | ||
| }> = Test; | ||
| let y: Test<{ | ||
| Foo { | ||
| value: 3, | ||
| nested: &Bar(5), | ||
| } | ||
| }> = x; //~ ERROR mismatched types | ||
| } |
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/issue-66451.rs:27:10 | ||
| | | ||
| LL | let y: Test<{ | ||
| | ____________- | ||
| LL | | Foo { | ||
| LL | | value: 3, | ||
| LL | | nested: &Bar(5), | ||
| LL | | } | ||
| LL | | }> = x; | ||
| | | - ^ expected `Foo { value: 3_i32, nested: &Bar::<i32>(5_i32) }`, found `Foo { value: 3_i32, nested: &Bar::<i32>(4_i32) }` | ||
| | |______| | ||
| | expected due to this | ||
| | | ||
| = note: expected struct `Test<Foo { value: 3_i32, nested: &Bar::<i32>(5_i32) }>` | ||
| found struct `Test<Foo { value: 3_i32, nested: &Bar::<i32>(4_i32) }>` | ||
|
|
||
| error: aborting due to previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0308`. |
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.
Uh oh!
There was an error while loading. Please reload this page.