Skip to content

Commit 988be9b

Browse files
xunilrjJoshuaBatty
andauthored
Fix issue with __transmute type checking (#6940)
## Description This PR solves an issue with `__transmute` in "const contexts". In these cases, the "source" and "return" types were not being correctly solved. ## Checklist - [ ] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
1 parent 6de04fc commit 988be9b

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

sway-core/src/ir_generation/const_eval.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,14 @@ fn const_eval_intrinsic(
14311431
value: &ConstantValue,
14321432
) -> Result<(), ConstEvalError> {
14331433
match t.get_content(ctx) {
1434+
TypeContent::Struct(fields) => match value {
1435+
ConstantValue::Struct(constants) => {
1436+
for (field_type, field) in fields.iter().zip(constants.iter()) {
1437+
append_bytes(ctx, bytes, field_type, &field.value)?;
1438+
}
1439+
}
1440+
_ => unreachable!(),
1441+
},
14341442
TypeContent::Array(item_type, size) => match value {
14351443
ConstantValue::Array(items) => {
14361444
assert!(*size as usize == items.len());

sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -196,24 +196,33 @@ fn type_check_transmute(
196196
forbid_ref_ptr_types(engines, handler, return_type, &type_arguments[1].span)?;
197197

198198
// check first argument
199-
let arg_type = engines.te().get(src_type);
199+
let arg_type = engines.te().new_unknown();
200200
let first_argument_typed_expr = {
201201
let ctx = ctx
202202
.by_ref()
203203
.with_help_text("")
204-
.with_type_annotation(engines.te().insert(
205-
engines,
206-
(*arg_type).clone(),
207-
type_arguments[0].span.source_id(),
208-
));
204+
.with_type_annotation(arg_type);
209205
ty::TyExpression::type_check(handler, ctx, &arguments[0]).unwrap()
210206
};
211207

208+
engines.te().unify(
209+
handler,
210+
engines,
211+
first_argument_typed_expr.return_type,
212+
src_type,
213+
&first_argument_typed_expr.span,
214+
"",
215+
None,
216+
);
217+
218+
let mut final_type_arguments = type_arguments.to_vec();
219+
final_type_arguments[0].type_id = src_type;
220+
final_type_arguments[1].type_id = return_type;
212221
Ok((
213222
TyIntrinsicFunctionKind {
214223
kind,
215224
arguments: vec![first_argument_typed_expr],
216-
type_arguments: type_arguments.to_vec(),
225+
type_arguments: final_type_arguments,
217226
span,
218227
},
219228
return_type,

test/src/e2e_vm_tests/test_programs/should_pass/language/const_inits/src/main.sw

+11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ const CARR1 = [X_SIZE - Y_SIZE + 1; 4];
9595
// and the type checker needs to know the size of the array.
9696
// const CARR2 = [1; X_SIZE - Y_SIZE + 1];
9797

98+
// Const init with Self
99+
struct WithSelf { value: u64 }
100+
impl WithSelf {
101+
pub fn size() -> u64 {
102+
__transmute::<Self, u64>(Self { value: 1u64 })
103+
}
104+
}
105+
const WithSelfValue: u64 = WithSelf::size();
106+
98107
fn main() -> u64 {
99108
const int1 = 1;
100109
assert(int1 == INT1 && ZERO_B256 == KEY);
@@ -158,6 +167,8 @@ fn main() -> u64 {
158167

159168
test_not();
160169

170+
assert(WithSelfValue == 1);
171+
161172
1
162173
}
163174

0 commit comments

Comments
 (0)