Skip to content
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
14 changes: 11 additions & 3 deletions compiler/rustc_lint/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/{$krate}/ptr/index.html#provenance>"
)]
#[note("for more information, visit <https://doc.rust-lang.org/std/ptr/index.html#provenance>")]
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<Int2PtrSuggestion>,
}
Expand All @@ -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 <https://doc.rust-lang.org/std/ptr/index.html#provenance>")]
#[note(
"for more information, visit <https://doc.rust-lang.org/{$krate}/ptr/index.html#provenance>"
)]
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<Ptr2IntSuggestion<'tcx>>,
}
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_lint/src/implicit_provenance_casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::diagnostics::{
ImplicitProvenanceCastsInt2Ptr, ImplicitProvenanceCastsPtr2Int, Int2PtrSuggestion,
Ptr2IntSuggestion,
};
use crate::utils::std_or_core;
use crate::{LateContext, LateLintPass};

declare_lint! {
Expand Down Expand Up @@ -69,19 +70,22 @@ 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)
}
}
}
}

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>,
Expand All @@ -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>,
Expand All @@ -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)
}
16 changes: 16 additions & 0 deletions tests/ui/lint/lint-strict-provenance-no_std.rs
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions tests/ui/lint/lint-strict-provenance-no_std.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/core/ptr/index.html#provenance>
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 <https://doc.rust-lang.org/core/ptr/index.html#provenance>

warning: 2 warnings emitted

Loading