Skip to content

Commit 0d54fe0

Browse files
committed
Remove a use of StructuredDiag, which is incompatible with automatic error tainting and error translations
1 parent 486bc27 commit 0d54fe0

File tree

9 files changed

+43
-78
lines changed

9 files changed

+43
-78
lines changed

Diff for: compiler/rustc_hir_analysis/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ hir_analysis_cannot_capture_late_bound_ty =
5555
cannot capture late-bound type parameter in {$what}
5656
.label = parameter defined here
5757
58-
hir_analysis_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}`
59-
6058
hir_analysis_closure_implicit_hrtb = implicit types in closure signatures are forbidden when `for<...>` is present
6159
.label = `for<...>` is here
6260

Diff for: compiler/rustc_hir_analysis/src/errors.rs

-9
Original file line numberDiff line numberDiff line change
@@ -707,15 +707,6 @@ pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
707707
pub help: Option<()>,
708708
}
709709

710-
#[derive(Diagnostic)]
711-
#[diag(hir_analysis_cast_thin_pointer_to_fat_pointer, code = E0607)]
712-
pub(crate) struct CastThinPointerToFatPointer<'tcx> {
713-
#[primary_span]
714-
pub span: Span,
715-
pub expr_ty: Ty<'tcx>,
716-
pub cast_ty: String,
717-
}
718-
719710
#[derive(Diagnostic)]
720711
#[diag(hir_analysis_invalid_union_field, code = E0740)]
721712
pub(crate) struct InvalidUnionField {

Diff for: compiler/rustc_hir_analysis/src/structured_errors.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
mod missing_cast_for_variadic_arg;
2-
mod sized_unsized_cast;
32
mod wrong_number_of_generic_args;
43

5-
pub use self::{
6-
missing_cast_for_variadic_arg::*, sized_unsized_cast::*, wrong_number_of_generic_args::*,
7-
};
4+
pub use self::{missing_cast_for_variadic_arg::*, wrong_number_of_generic_args::*};
85

96
use rustc_errors::{Diag, ErrCode};
107
use rustc_session::Session;

Diff for: compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs

-56
This file was deleted.

Diff for: compiler/rustc_hir_typeck/messages.ftl

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ hir_typeck_cannot_cast_to_bool = cannot cast `{$expr_ty}` as `bool`
2323
2424
hir_typeck_cast_enum_drop = cannot cast enum `{$expr_ty}` into integer `{$cast_ty}` because it implements `Drop`
2525
26+
hir_typeck_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}`
27+
.teach_help = Thin pointers are "simple" pointers: they are purely a reference to a
28+
memory address.
29+
30+
Fat pointers are pointers referencing "Dynamically Sized Types" (also
31+
called DST). DST don't have a statically known size, therefore they can
32+
only exist behind some kind of pointers that contain additional
33+
information. Slices and trait objects are DSTs. In the case of slices,
34+
the additional information the fat pointer holds is their size.
35+
36+
To fix this error, don't try to cast directly between thin and fat
37+
pointers.
38+
39+
For more information about casts, take a look at The Book:
40+
https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",
2641
hir_typeck_cast_unknown_pointer = cannot cast {$to ->
2742
[true] to
2843
*[false] from

Diff for: compiler/rustc_hir_typeck/src/cast.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,12 @@ impl<'a, 'tcx> CastCheck<'tcx> {
500500
err.emit();
501501
}
502502
CastError::SizedUnsizedCast => {
503-
use rustc_hir_analysis::structured_errors::{SizedUnsizedCast, StructuredDiag};
504-
505-
SizedUnsizedCast {
506-
sess: fcx.tcx.sess,
503+
fcx.dcx().emit_err(errors::CastThinPointerToFatPointer {
507504
span: self.span,
508505
expr_ty: self.expr_ty,
509506
cast_ty: fcx.ty_to_string(self.cast_ty),
510-
}
511-
.diagnostic()
512-
.emit();
507+
teach: fcx.tcx.sess.teach(E0607).then_some(()),
508+
});
513509
}
514510
CastError::IntToFatCast(known_metadata) => {
515511
let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);

Diff for: compiler/rustc_hir_typeck/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,14 @@ pub struct ReplaceWithName {
689689
pub span: Span,
690690
pub name: String,
691691
}
692+
693+
#[derive(Diagnostic)]
694+
#[diag(hir_typeck_cast_thin_pointer_to_fat_pointer, code = E0607)]
695+
pub(crate) struct CastThinPointerToFatPointer<'tcx> {
696+
#[primary_span]
697+
pub span: Span,
698+
pub expr_ty: Ty<'tcx>,
699+
pub cast_ty: String,
700+
#[note(hir_typeck_teach_help)]
701+
pub(crate) teach: Option<()>,
702+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const FOO: &str = unsafe { &*(1_usize as *const [i64; 0] as *const [u8] as *const str) };
2+
//~^ ERROR: cannot cast
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0607]: cannot cast thin pointer `*const [i64; 0]` to fat pointer `*const [u8]`
2+
--> $DIR/slice_elem_ty_mismatch_in_unsizing_cast.rs:1:31
3+
|
4+
LL | const FOO: &str = unsafe { &*(1_usize as *const [i64; 0] as *const [u8] as *const str) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0607`.

0 commit comments

Comments
 (0)