check if len of array const arg matches the expected len of the type when lowering to valtree - #158587
check if len of array const arg matches the expected len of the type when lowering to valtree#158587sjwang05 wants to merge 1 commit into
Conversation
|
HIR ty lowering was modified cc @fmease |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| .map(|elem| self.lower_const_arg(elem, *elem_ty)) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| if let Some(expected_len) = len.try_to_target_usize(tcx) |
There was a problem hiding this comment.
IMO it should catch more cases if len is normalized, e.g. type const LEN (I haven't checked deeply so it may be caught by somewhere already)
There was a problem hiding this comment.
Thanks, I changed it to call try_normalize_erasing_regions with an empty ParamEnv on the expected len and added a test for it, so now something like fn baz<const A: [u8; <S as Trait>::LEN]>() {} produces the correct error. Something like fn baz<T: Trait, const A: [u8; <T as Trait>::LEN]>() {} still ICEs if too long/produces UB if too short, however, since we can't resolve T to an actual type: trying to do try_evaluate_const with a non-empty ParamEnv led to cycle errors.
There was a problem hiding this comment.
Could you open another issue and add an ICE test for that case (if not exists yet)? At least we should track it.
When creating valtrees for arrays passed as const args in
lower_const_arg_array, we assume the array has the correct length without actually checking the length of the arg, leading to UB at runtime (#155168) or an ICE during CTFE (#151079). This PR adds a check comparing the expected length of the type with the actual number of elements, similar tolower_const_arg_tup.fixes #155168