Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove a use of StructuredDiag, which is incompatible with automatic error tainting and error translations #127319

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ hir_analysis_cannot_capture_late_bound_ty =
cannot capture late-bound type parameter in {$what}
.label = parameter defined here

hir_analysis_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}`

hir_analysis_closure_implicit_hrtb = implicit types in closure signatures are forbidden when `for<...>` is present
.label = `for<...>` is here

Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,15 +707,6 @@ pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
pub help: Option<()>,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_cast_thin_pointer_to_fat_pointer, code = E0607)]
pub(crate) struct CastThinPointerToFatPointer<'tcx> {
#[primary_span]
pub span: Span,
pub expr_ty: Ty<'tcx>,
pub cast_ty: String,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_invalid_union_field, code = E0740)]
pub(crate) struct InvalidUnionField {
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_hir_analysis/src/structured_errors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
mod missing_cast_for_variadic_arg;
mod sized_unsized_cast;
mod wrong_number_of_generic_args;

pub use self::{
missing_cast_for_variadic_arg::*, sized_unsized_cast::*, wrong_number_of_generic_args::*,
};
pub use self::{missing_cast_for_variadic_arg::*, wrong_number_of_generic_args::*};

use rustc_errors::{Diag, ErrCode};
use rustc_session::Session;
Expand Down

This file was deleted.

15 changes: 15 additions & 0 deletions compiler/rustc_hir_typeck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ hir_typeck_cannot_cast_to_bool = cannot cast `{$expr_ty}` as `bool`

hir_typeck_cast_enum_drop = cannot cast enum `{$expr_ty}` into integer `{$cast_ty}` because it implements `Drop`

hir_typeck_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}`
.teach_help = Thin pointers are "simple" pointers: they are purely a reference to a
memory address.

Fat pointers are pointers referencing "Dynamically Sized Types" (also
called DST). DST don't have a statically known size, therefore they can
only exist behind some kind of pointers that contain additional
information. Slices and trait objects are DSTs. In the case of slices,
the additional information the fat pointer holds is their size.

To fix this error, don't try to cast directly between thin and fat
pointers.

For more information about casts, take a look at The Book:
https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",
hir_typeck_cast_unknown_pointer = cannot cast {$to ->
[true] to
*[false] from
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,16 +500,12 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.emit();
}
CastError::SizedUnsizedCast => {
use rustc_hir_analysis::structured_errors::{SizedUnsizedCast, StructuredDiag};

SizedUnsizedCast {
sess: fcx.tcx.sess,
fcx.dcx().emit_err(errors::CastThinPointerToFatPointer {
span: self.span,
expr_ty: self.expr_ty,
cast_ty: fcx.ty_to_string(self.cast_ty),
}
.diagnostic()
.emit();
teach: fcx.tcx.sess.teach(E0607).then_some(()),
});
}
CastError::IntToFatCast(known_metadata) => {
let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,14 @@ pub struct ReplaceWithName {
pub span: Span,
pub name: String,
}

#[derive(Diagnostic)]
#[diag(hir_typeck_cast_thin_pointer_to_fat_pointer, code = E0607)]
pub(crate) struct CastThinPointerToFatPointer<'tcx> {
#[primary_span]
pub span: Span,
pub expr_ty: Ty<'tcx>,
pub cast_ty: String,
#[note(hir_typeck_teach_help)]
pub(crate) teach: Option<()>,
}
4 changes: 4 additions & 0 deletions tests/ui/consts/slice_elem_ty_mismatch_in_unsizing_cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const FOO: &str = unsafe { &*(1_usize as *const [i64; 0] as *const [u8] as *const str) };
//~^ ERROR: cannot cast

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0607]: cannot cast thin pointer `*const [i64; 0]` to fat pointer `*const [u8]`
--> $DIR/slice_elem_ty_mismatch_in_unsizing_cast.rs:1:31
|
LL | const FOO: &str = unsafe { &*(1_usize as *const [i64; 0] as *const [u8] as *const str) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0607`.
Loading