-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #98643 - voidc:valtree-ref-pretty, r=lcnr
Improve pretty printing of valtrees for references This implements the changes outlined in #66451 (comment). r? `@lcnr` Fixes #66451
- Loading branch information
Showing
9 changed files
with
138 additions
and
102 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use rustc_middle::ty::{self, TyCtxt}; | ||
use rustc_target::abi::VariantIdx; | ||
|
||
use std::iter; | ||
|
||
/// Destructures array, ADT or tuple constants into the constants | ||
/// of their fields. | ||
pub(crate) fn destructure_const<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
const_: ty::Const<'tcx>, | ||
) -> ty::DestructuredConst<'tcx> { | ||
let ty::ConstKind::Value(valtree) = const_.kind() else { | ||
bug!("cannot destructure constant {:?}", const_) | ||
}; | ||
|
||
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, branches) = if def.is_enum() { | ||
let (head, rest) = branches.split_first().unwrap(); | ||
(VariantIdx::from_u32(head.unwrap_leaf().try_to_u32().unwrap()), rest) | ||
} else { | ||
(VariantIdx::from_u32(0), branches) | ||
}; | ||
let fields = &def.variant(variant_idx).fields; | ||
let mut field_consts = Vec::with_capacity(fields.len()); | ||
|
||
for (field, field_valtree) in iter::zip(fields, branches) { | ||
let field_ty = field.ty(tcx, substs); | ||
let field_const = tcx.mk_const(ty::ConstS { | ||
kind: ty::ConstKind::Value(*field_valtree), | ||
ty: field_ty, | ||
}); | ||
field_consts.push(field_const); | ||
} | ||
debug!(?field_consts); | ||
|
||
(field_consts, Some(variant_idx)) | ||
} | ||
ty::Tuple(elem_tys) => { | ||
let fields = iter::zip(*elem_tys, branches) | ||
.map(|(elem_ty, elem_valtree)| { | ||
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 } | ||
} | ||
|
||
pub fn provide(providers: &mut ty::query::Providers) { | ||
*providers = ty::query::Providers { destructure_const, ..*providers }; | ||
} |
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 |
---|---|---|
@@ -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 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`. |