Auto merge of #98680 - matthiaskrgr:rollup-1bkrrn9, r=matthiaskrgr · rust-lang/rust@ddcbba0 (original) (raw)

`@@ -5,7 +5,6 @@ use rustc_middle::mir;

`

5

5

`use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};

`

6

6

`use rustc_middle::ty::{self, TyCtxt};

`

7

7

`use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};

`

8

``

`-

use rustc_target::abi::VariantIdx;

`

9

8

``

10

9

`use crate::interpret::{

`

11

10

` intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MemPlaceMeta,

`

`@@ -91,83 +90,6 @@ pub(crate) fn eval_to_valtree<'tcx>(

`

91

90

`}

`

92

91

`}

`

93

92

``

94

``

`-

/// Tries to destructure constants of type Array or Adt into the constants

`

95

``

`-

/// of its fields.

`

96

``

`-

pub(crate) fn try_destructure_const<'tcx>(

`

97

``

`-

tcx: TyCtxt<'tcx>,

`

98

``

`-

const_: ty::Const<'tcx>,

`

99

``

`-

) -> Option<ty::DestructuredConst<'tcx>> {

`

100

``

`-

if let ty::ConstKind::Value(valtree) = const_.kind() {

`

101

``

`-

let branches = match valtree {

`

102

``

`-

ty::ValTree::Branch(b) => b,

`

103

``

`-

_ => return None,

`

104

``

`-

};

`

105

``

-

106

``

`-

let (fields, variant) = match const_.ty().kind() {

`

107

``

`-

ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {

`

108

``

`-

// construct the consts for the elements of the array/slice

`

109

``

`-

let field_consts = branches

`

110

``

`-

.iter()

`

111

``

`-

.map(|b| {

`

112

``

`-

tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Value(*b), ty: *inner_ty })

`

113

``

`-

})

`

114

``

`-

.collect::<Vec<_>>();

`

115

``

`-

debug!(?field_consts);

`

116

``

-

117

``

`-

(field_consts, None)

`

118

``

`-

}

`

119

``

`-

ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),

`

120

``

`-

ty::Adt(def, substs) => {

`

121

``

`-

let variant_idx = if def.is_enum() {

`

122

``

`-

VariantIdx::from_u32(branches[0].unwrap_leaf().try_to_u32().ok()?)

`

123

``

`-

} else {

`

124

``

`-

VariantIdx::from_u32(0)

`

125

``

`-

};

`

126

``

`-

let fields = &def.variant(variant_idx).fields;

`

127

``

`-

let mut field_consts = Vec::with_capacity(fields.len());

`

128

``

-

129

``

`-

// Note: First element inValTree corresponds to variant of enum

`

130

``

`-

let mut valtree_idx = if def.is_enum() { 1 } else { 0 };

`

131

``

`-

for field in fields {

`

132

``

`-

let field_ty = field.ty(tcx, substs);

`

133

``

`-

let field_valtree = branches[valtree_idx]; // first element of branches is variant

`

134

``

`-

let field_const = tcx.mk_const(ty::ConstS {

`

135

``

`-

kind: ty::ConstKind::Value(field_valtree),

`

136

``

`-

ty: field_ty,

`

137

``

`-

});

`

138

``

`-

field_consts.push(field_const);

`

139

``

`-

valtree_idx += 1;

`

140

``

`-

}

`

141

``

`-

debug!(?field_consts);

`

142

``

-

143

``

`-

(field_consts, Some(variant_idx))

`

144

``

`-

}

`

145

``

`-

ty::Tuple(elem_tys) => {

`

146

``

`-

let fields = elem_tys

`

147

``

`-

.iter()

`

148

``

`-

.enumerate()

`

149

``

`-

.map(|(i, elem_ty)| {

`

150

``

`-

let elem_valtree = branches[i];

`

151

``

`-

tcx.mk_const(ty::ConstS {

`

152

``

`-

kind: ty::ConstKind::Value(elem_valtree),

`

153

``

`-

ty: elem_ty,

`

154

``

`-

})

`

155

``

`-

})

`

156

``

`-

.collect::<Vec<_>>();

`

157

``

-

158

``

`-

(fields, None)

`

159

``

`-

}

`

160

``

`-

_ => bug!("cannot destructure constant {:?}", const_),

`

161

``

`-

};

`

162

``

-

163

``

`-

let fields = tcx.arena.alloc_from_iter(fields.into_iter());

`

164

``

-

165

``

`-

Some(ty::DestructuredConst { variant, fields })

`

166

``

`-

} else {

`

167

``

`-

None

`

168

``

`-

}

`

169

``

`-

}

`

170

``

-

171

93

`#[instrument(skip(tcx), level = "debug")]

`

172

94

`pub(crate) fn try_destructure_mir_constant<'tcx>(

`

173

95

`tcx: TyCtxt<'tcx>,

`