Skip to content

Commit 0b99f29

Browse files
committed
Auto merge of rust-lang#18217 - ChayimFriedman2:cast-unknown-ptr, r=Veykril
fix: Comment out cast checks for unknown ptr kind Just like we don't check for types containing unknown. Fixes rust-lang#18214. See also https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/Another.20case.20of.20.2318064.3F.
2 parents 540891b + 8115a18 commit 0b99f29

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer/cast.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ pub enum CastError {
7070
NeedViaThinPtr,
7171
NeedViaInt,
7272
NonScalar,
73-
UnknownCastPtrKind,
74-
UnknownExprPtrKind,
73+
// We don't want to report errors with unknown types currently.
74+
// UnknownCastPtrKind,
75+
// UnknownExprPtrKind,
7576
}
7677

7778
impl CastError {
@@ -272,9 +273,10 @@ impl CastCheck {
272273

273274
match (src_kind, dst_kind) {
274275
(Some(PointerKind::Error), _) | (_, Some(PointerKind::Error)) => Ok(()),
275-
(_, None) => Err(CastError::UnknownCastPtrKind),
276+
// (_, None) => Err(CastError::UnknownCastPtrKind),
277+
// (None, _) => Err(CastError::UnknownExprPtrKind),
278+
(_, None) | (None, _) => Ok(()),
276279
(_, Some(PointerKind::Thin)) => Ok(()),
277-
(None, _) => Err(CastError::UnknownExprPtrKind),
278280
(Some(PointerKind::Thin), _) => Err(CastError::SizedUnsizedCast),
279281
(Some(PointerKind::VTable(src_tty)), Some(PointerKind::VTable(dst_tty))) => {
280282
let principal = |tty: &Binders<QuantifiedWhereClauses>| {
@@ -315,7 +317,8 @@ impl CastCheck {
315317
expr_ty: &Ty,
316318
) -> Result<(), CastError> {
317319
match pointer_kind(expr_ty, table).map_err(|_| CastError::Unknown)? {
318-
None => Err(CastError::UnknownExprPtrKind),
320+
// None => Err(CastError::UnknownExprPtrKind),
321+
None => Ok(()),
319322
Some(PointerKind::Error) => Ok(()),
320323
Some(PointerKind::Thin) => Ok(()),
321324
_ => Err(CastError::NeedViaThinPtr),
@@ -328,7 +331,8 @@ impl CastCheck {
328331
cast_ty: &Ty,
329332
) -> Result<(), CastError> {
330333
match pointer_kind(cast_ty, table).map_err(|_| CastError::Unknown)? {
331-
None => Err(CastError::UnknownCastPtrKind),
334+
// None => Err(CastError::UnknownCastPtrKind),
335+
None => Ok(()),
332336
Some(PointerKind::Error) => Ok(()),
333337
Some(PointerKind::Thin) => Ok(()),
334338
Some(PointerKind::VTable(_)) => Err(CastError::IntToFatCast),
@@ -343,7 +347,8 @@ impl CastCheck {
343347
cast_ty: &Ty,
344348
) -> Result<(), CastError> {
345349
match pointer_kind(cast_ty, table).map_err(|_| CastError::Unknown)? {
346-
None => Err(CastError::UnknownCastPtrKind),
350+
// None => Err(CastError::UnknownCastPtrKind),
351+
None => Ok(()),
347352
Some(PointerKind::Error) => Ok(()),
348353
Some(PointerKind::Thin) => Ok(()),
349354
_ => Err(CastError::IllegalCast),

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/invalid_cast.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ pub(crate) fn invalid_cast(ctx: &DiagnosticsContext<'_>, d: &hir::InvalidCast) -
9595
DiagnosticCode::RustcHardError("E0605"),
9696
format_ty!(ctx, "non-primitive cast: `{}` as `{}`", d.expr_ty, d.cast_ty),
9797
),
98-
CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => (
99-
DiagnosticCode::RustcHardError("E0641"),
100-
"cannot cast to a pointer of an unknown kind".to_owned(),
101-
),
98+
// CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => (
99+
// DiagnosticCode::RustcHardError("E0641"),
100+
// "cannot cast to a pointer of an unknown kind".to_owned(),
101+
// ),
102102
};
103103
Diagnostic::new(code, message, display_range)
104104
}
@@ -457,20 +457,20 @@ fn foo<T: ?Sized>() {
457457
);
458458
}
459459

460-
#[test]
461-
fn order_dependent_cast_inference() {
462-
check_diagnostics(
463-
r#"
464-
//- minicore: sized
465-
fn main() {
466-
let x = &"hello";
467-
let mut y = 0 as *const _;
468-
//^^^^^^^^^^^^^ error: cannot cast to a pointer of an unknown kind
469-
y = x as *const _;
470-
}
471-
"#,
472-
);
473-
}
460+
// #[test]
461+
// fn order_dependent_cast_inference() {
462+
// check_diagnostics(
463+
// r#"
464+
// //- minicore: sized
465+
// fn main() {
466+
// let x = &"hello";
467+
// let mut y = 0 as *const _;
468+
// //^^^^^^^^^^^^^ error: cannot cast to a pointer of an unknown kind
469+
// y = x as *const _;
470+
// }
471+
// "#,
472+
// );
473+
// }
474474

475475
#[test]
476476
fn ptr_to_ptr_different_regions() {
@@ -1111,4 +1111,22 @@ fn foo() {
11111111
"#,
11121112
);
11131113
}
1114+
1115+
#[test]
1116+
fn cast_isize_to_infer_pointer() {
1117+
check_diagnostics(
1118+
r#"
1119+
//- minicore: coerce_unsized
1120+
struct Foo {}
1121+
1122+
struct Wrap<'a>(&'a mut Foo);
1123+
1124+
fn main() {
1125+
let lparam: isize = 0;
1126+
1127+
let _wrap = Wrap(unsafe { &mut *(lparam as *mut _) });
1128+
}
1129+
"#,
1130+
);
1131+
}
11141132
}

0 commit comments

Comments
 (0)