diff options
author | Philip Herron <herron.philip@googlemail.com> | 2023-06-29 10:13:01 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-06-29 15:38:44 +0000 |
commit | 7af86eaaffec8cab5ad80e242480fe9f6c1ffaa6 (patch) | |
tree | d27ca036110d5a62c0f0f08990e3eeba9e6da87a /gcc/rust/hir | |
parent | eb88e324cde1e832dc6f3a5932aef9ee8e7abf6a (diff) | |
download | gcc-7af86eaaffec8cab5ad80e242480fe9f6c1ffaa6.zip gcc-7af86eaaffec8cab5ad80e242480fe9f6c1ffaa6.tar.gz gcc-7af86eaaffec8cab5ad80e242480fe9f6c1ffaa6.tar.bz2 |
gccrs: fix how we handle coercions and casts of unsafe pointers
I copied swap_nonoverlapping badly from libcore which uses an unsafe
pointer case of x as *mut u8, I thought this was x as *mut T when i was
copying code form libcore this caused new infererence variables to be
introduced which actually automatically turned this swap_nonoverlapping
function to turn into a swap_nonoverlapping<u8> and lost the general
generic one in the type system context.
Rust allows for you to cast any pointer to another pointer of different
base type. The rules here are really subtle though because we need to
handle the case where we do really need to unify the types here there
are a few cases to consider but the main three are:
*u32 vs *u32 -> valid pointers match = simple coercion
*<?> vs *u8 -> inference variable unified with u8 which is valid
*T vs *u8 -> invalid coercion as the element types don't match
-> But this is a valid cast site
The code for casts try a coercion first then if that was sucsessful goes
on to perform a real coercion. Otherwise it follows the cast rules. The
bug here was that we saw the case of *T vs *u8 the try coercion used
inference variables to match T vs u8 but this will cause a cascase of
bad inference variables which we don't want when we perform the simple
coercion.
Fixes #2330
gcc/rust/ChangeLog:
* typecheck/rust-casts.cc (TypeCastRules::check): apply new argument
* typecheck/rust-coercion.cc (TypeCoercionRules::Coerce): track if this is a cast site
(TypeCoercionRules::TryCoerce): update ctor
(TypeCoercionRules::TypeCoercionRules): cleanup error handling
(TypeCoercionRules::do_coercion): likewise
(TypeCoercionRules::coerce_unsafe_ptr): only infer in try mode
(TypeCoercionRules::coerce_borrowed_pointer): cleanup
* typecheck/rust-coercion.h: update header
gcc/testsuite/ChangeLog:
* rust/compile/issue-1981.rs: Fix badly copied libcore code test
* rust/compile/issue-2330.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust/hir')
0 files changed, 0 insertions, 0 deletions