diff --git a/compiler/rustc_lint/src/diagnostics.rs b/compiler/rustc_lint/src/diagnostics.rs index 69cdee19a56fb..0e52c041e38a1 100644 --- a/compiler/rustc_lint/src/diagnostics.rs +++ b/compiler/rustc_lint/src/diagnostics.rs @@ -3145,12 +3145,16 @@ pub(crate) struct EqInternalMethodImplemented; #[derive(Diagnostic)] #[diag("cast from `{$expr_ty}` to `{$cast_ty}` implicitly relies on exposed provenance")] #[help( - "if conforming to strict provenance is not possible, use `std::ptr::with_exposed_provenance()`" + "if conforming to strict provenance is not possible, use `{$krate}::ptr::with_exposed_provenance()`" +)] +#[note( + "for more information, visit " )] -#[note("for more information, visit ")] pub(crate) struct ImplicitProvenanceCastsInt2Ptr<'tcx> { pub expr_ty: Ty<'tcx>, pub cast_ty: Ty<'tcx>, + /// Either `std` or `core`. + pub krate: &'static str, #[subdiagnostic] pub sugg: Option, } @@ -3170,10 +3174,14 @@ pub(crate) struct Int2PtrSuggestion { #[derive(Diagnostic)] #[diag("cast from `{$cast_from_ty}` to `{$cast_to_ty}` implicitly exposes pointer provenance")] #[help("if conforming to strict provenance is not possible, use `.expose_provenance()`")] -#[note("for more information, visit ")] +#[note( + "for more information, visit " +)] pub(crate) struct ImplicitProvenanceCastsPtr2Int<'tcx> { pub cast_from_ty: Ty<'tcx>, pub cast_to_ty: Ty<'tcx>, + /// Either `std` or `core`, used to link to the matching documentation. + pub krate: &'static str, #[subdiagnostic] pub sugg: Option>, } diff --git a/compiler/rustc_lint/src/implicit_provenance_casts.rs b/compiler/rustc_lint/src/implicit_provenance_casts.rs index f1646f2cc6072..470fe36ef5f5b 100644 --- a/compiler/rustc_lint/src/implicit_provenance_casts.rs +++ b/compiler/rustc_lint/src/implicit_provenance_casts.rs @@ -7,6 +7,7 @@ use crate::diagnostics::{ ImplicitProvenanceCastsInt2Ptr, ImplicitProvenanceCastsPtr2Int, Int2PtrSuggestion, Ptr2IntSuggestion, }; +use crate::utils::std_or_core; use crate::{LateContext, LateLintPass}; declare_lint! { @@ -69,12 +70,14 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitProvenanceCasts { if cast_from_ty.is_raw_ptr() { let cast_to_ty = typeck_results.expr_ty(expr); if cast_to_ty.is_integral() { - lint_ptr2int(cx, expr, cast_from_expr, cast_from_ty, cast_to_hir, cast_to_ty) + let krate = std_or_core(cx).unwrap_or("core"); + lint_ptr2int(cx, krate, expr, cast_from_expr, cast_from_ty, cast_to_hir, cast_to_ty) } } else if cast_from_ty.is_integral() { let cast_to_ty = typeck_results.expr_ty(expr); if cast_to_ty.is_raw_ptr() { - lint_int2ptr(cx, expr, cast_from_expr, cast_from_ty, cast_to_hir, cast_to_ty) + let krate = std_or_core(cx).unwrap_or("core"); + lint_int2ptr(cx, krate, expr, cast_from_expr, cast_from_ty, cast_to_hir, cast_to_ty) } } } @@ -82,6 +85,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitProvenanceCasts { fn lint_ptr2int<'tcx>( cx: &LateContext<'tcx>, + krate: &'static str, expr: &'tcx hir::Expr<'tcx>, cast_from_expr: &'tcx hir::Expr<'tcx>, cast_from_ty: Ty<'tcx>, @@ -101,12 +105,13 @@ fn lint_ptr2int<'tcx>( } }); - let lint = ImplicitProvenanceCastsPtr2Int { cast_from_ty, cast_to_ty, sugg }; + let lint = ImplicitProvenanceCastsPtr2Int { cast_from_ty, cast_to_ty, krate, sugg }; cx.tcx.emit_node_span_lint(IMPLICIT_PROVENANCE_CASTS, expr.hir_id, expr.span, lint); } fn lint_int2ptr<'tcx>( cx: &LateContext<'tcx>, + krate: &'static str, expr: &'tcx hir::Expr<'tcx>, cast_from_expr: &'tcx hir::Expr<'tcx>, cast_from_ty: Ty<'tcx>, @@ -117,6 +122,7 @@ fn lint_int2ptr<'tcx>( lo: cast_from_expr.span.shrink_to_lo(), hi: cast_from_expr.span.shrink_to_hi().to(cast_to_hir.span), }); - let lint = ImplicitProvenanceCastsInt2Ptr { expr_ty: cast_from_ty, cast_ty: cast_to_ty, sugg }; + let lint = + ImplicitProvenanceCastsInt2Ptr { expr_ty: cast_from_ty, cast_ty: cast_to_ty, krate, sugg }; cx.tcx.emit_node_span_lint(IMPLICIT_PROVENANCE_CASTS, expr.hir_id, expr.span, lint) } diff --git a/tests/ui/lint/lint-strict-provenance-no_std.rs b/tests/ui/lint/lint-strict-provenance-no_std.rs new file mode 100644 index 0000000000000..6b0aaf6db22fe --- /dev/null +++ b/tests/ui/lint/lint-strict-provenance-no_std.rs @@ -0,0 +1,16 @@ +//@ check-pass + +#![no_std] +#![crate_type = "lib"] +#![feature(strict_provenance_lints)] +#![warn(implicit_provenance_casts)] + +pub fn int2ptr() -> *const u8 { + 16_usize as *const u8 + //~^ WARN cast from `usize` to `*const u8` implicitly relies on exposed provenance +} + +pub fn ptr2int(ptr: *const u8) -> usize { + ptr as usize + //~^ WARN cast from `*const u8` to `usize` implicitly exposes pointer provenance +} diff --git a/tests/ui/lint/lint-strict-provenance-no_std.stderr b/tests/ui/lint/lint-strict-provenance-no_std.stderr new file mode 100644 index 0000000000000..58492acd91c59 --- /dev/null +++ b/tests/ui/lint/lint-strict-provenance-no_std.stderr @@ -0,0 +1,32 @@ +warning: cast from `usize` to `*const u8` implicitly relies on exposed provenance + --> $DIR/lint-strict-provenance-no_std.rs:9:5 + | +LL | 16_usize as *const u8 + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: if conforming to strict provenance is not possible, use `core::ptr::with_exposed_provenance()` + = note: for more information, visit +note: the lint level is defined here + --> $DIR/lint-strict-provenance-no_std.rs:6:9 + | +LL | #![warn(implicit_provenance_casts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +help: use `.with_addr()` to adjust the address of a valid pointer in the same allocation + | +LL - 16_usize as *const u8 +LL + (...).with_addr(16_usize) + | + +warning: cast from `*const u8` to `usize` implicitly exposes pointer provenance + --> $DIR/lint-strict-provenance-no_std.rs:14:5 + | +LL | ptr as usize + | ^^^--------- + | | + | help: use `.addr()` to obtain the address of a pointer: `.addr()` + | + = help: if conforming to strict provenance is not possible, use `.expose_provenance()` + = note: for more information, visit + +warning: 2 warnings emitted +