Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8b40773
Add `raw_borrows_via_references` lint
obeis Jun 24, 2026
ce2fc22
Add test for `raw_borrows_via_references` lint
obeis Jun 27, 2026
a67aa12
Use an explicit list of types for `RefDecodable` into arena
Zalathar Jul 26, 2026
08cb62d
Use the same list for `RefDecodable` of Copy types
Zalathar Jul 26, 2026
b0b25c9
Stop using higher-order macros to declare arenas
Zalathar Jul 26, 2026
01cef43
Remove the `[]` prefix from `declare_arena!` entries
Zalathar Jul 29, 2026
c977f6b
misc: use visitor macros to reduce boilerplate
ada4a Jul 29, 2026
afcea59
derive `GenericTypeVisitable` for `RegionConstraint`
ada4a Jul 29, 2026
952cbef
A few more "predicate"-to-"clause" renamings
ada4a Jul 29, 2026
b60abe4
Fix ICE for parsing issue with unexpected closing brace
chenyukang Jul 30, 2026
f54409b
Remove method `Subcommand::kind`
Zalathar Jul 30, 2026
84af593
std: make positioned I/O unsupported on VxWorks
JohnTitor Jul 27, 2026
2ac47fa
Rollup merge of #138230 - obeis:lint-unnecessary-reference, r=Urgau
JonathanBrouwer Jul 30, 2026
9eecf8a
Rollup merge of #160031 - JohnTitor:vxworks-pread-pwrite-unsupported,…
JonathanBrouwer Jul 30, 2026
448c0da
Rollup merge of #159955 - Zalathar:arena, r=mejrs
JonathanBrouwer Jul 30, 2026
85432d5
Rollup merge of #160164 - ada4a:push-qkmmvxwqwoun, r=lcnr
JonathanBrouwer Jul 30, 2026
dfd46d4
Rollup merge of #160177 - ada4a:push-xmqwrrzvqlny, r=nnethercote
JonathanBrouwer Jul 30, 2026
f9eab85
Rollup merge of #160192 - chenyukang:yukang-fix-160171-pub-before-clo…
JonathanBrouwer Jul 30, 2026
cddf530
Rollup merge of #160209 - Zalathar:subcommand-kind, r=Kobzol
JonathanBrouwer Jul 30, 2026
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
35 changes: 23 additions & 12 deletions compiler/rustc_arena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,32 @@ impl DroplessArena {
}
}

/// Declare an `Arena` containing one dropless arena and many typed arenas (the
/// types of the typed arenas are specified by the arguments).
/// Declares an `Arena` that can allocate values of a variety of `Copy`, `needs_drop` and
/// `!needs_drop` types.
///
/// There are three cases of interest.
/// - Types that are `Copy`: these need not be specified in the arguments. They
/// will use the `DroplessArena`.
/// - Types that are `!Copy` and `!Drop`: these must be specified in the
/// arguments. An empty `TypedArena` will be created for each one, but the
/// `DroplessArena` will always be used and the `TypedArena` will stay empty.
/// This is odd but harmless, because an empty arena allocates no memory.
/// - Types that are `!Copy` and `Drop`: these must be specified in the
/// arguments. The `TypedArena` will be used for them.
/// The declared arena actually contains a single [`DroplessArena`], plus a separate
/// [`TypedArena`] for each of the types listed in the body of the macro invocation.
///
/// Any type that is `Copy` can be allocated in the arena without needing to be listed
/// explicitly. Those values will be stored in the [`DroplessArena`].
///
/// Types that are `!Copy` can only be allocated if they are listed in the macro invocation.
/// For types that are `!Copy + needs_drop`, values will be stored in the corresponding
/// [`TypedArena`] and will be dropped when the arena is dropped.
///
/// As an optimization, types that are `!Copy + !needs_drop` will actually be stored in the
/// [`DroplessArena`], and the corresponding [`TypedArena`] will remain empty. This makes
/// better use of the dropless arena's storage blocks, while the overhead of having a few
/// unused typed-arenas is negligible.
#[rustc_macro_transparency = "semiopaque"]
pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
pub macro declare_arena(
// Each of these entries becomes a `$name: TypedArena<$ty>` field in the arena.
// This allows values of non-copy type $ty to be allocated in the arena.
// The field names must be distinct, but have no further significance.
$(
$name:ident: $ty:ty,
)*
) {
#[derive(Default)]
pub struct Arena<'tcx> {
pub dropless: $crate::DroplessArena,
Expand Down
24 changes: 10 additions & 14 deletions compiler/rustc_hir/src/arena.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/// This higher-order macro declares a list of types which can be allocated by `Arena`.
/// Note that all `Copy` types can be allocated by default and need not be specified here.
#[macro_export]
macro_rules! arena_types {
($macro:path) => (
$macro!([
// HIR types
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
[] attribute: crate::Attribute,
[] owner_info: crate::OwnerInfo<'tcx>,
[] macro_def: rustc_ast::MacroDef,
[] delegation_info: crate::DelegationInfo,
]);
)
//! Declares an arena that can allocate values of any `Copy` type, and of
//! any `!Copy` type listed below.

rustc_arena::declare_arena! {
// HIR types
asm_template: rustc_ast::InlineAsmTemplatePiece,
attribute: crate::Attribute,
owner_info: crate::OwnerInfo<'tcx>,
macro_def: rustc_ast::MacroDef,
delegation_info: crate::DelegationInfo,
}
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ pub use rustc_span::def_id;
pub use stability::*;
pub use target::{MethodKind, Target};

arena_types!(rustc_arena::declare_arena);
pub use crate::arena::Arena;
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
/// will give us `U: 'static` and `U: Outer`. The latter we
/// can ignore, but we will want to process `U: 'static`,
/// applying the instantiation as above.
// FIXME: change this function's signature and docs to mention clauses instead of predicates
#[tracing::instrument(level = "debug", skip(tcx))]
fn check_explicit_predicates<'tcx>(
tcx: TyCtxt<'tcx>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/outlives/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) type RequiredPredicates<'tcx> = FxIndexMap<ty::ArgOutlivesPredicate<'

/// Given a requirement `T: 'a` or `'b: 'a`, deduce the
/// outlives_component and add it to `required_predicates`
// FIXME: change this function's signature and docs to mention clauses instead of predicates
pub(crate) fn insert_outlives_predicate<'tcx>(
tcx: TyCtxt<'tcx>,
arg: GenericArg<'tcx>,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod opaque_hidden_inferred_bound;
mod passes;
mod precedence;
mod ptr_nulls;
mod raw_borrows_via_references;
mod redundant_semicolon;
mod reference_casting;
mod runtime_symbols;
Expand Down Expand Up @@ -117,6 +118,7 @@ use noop_method_call::*;
use opaque_hidden_inferred_bound::*;
use precedence::*;
use ptr_nulls::*;
use raw_borrows_via_references::*;
use redundant_semicolon::*;
use reference_casting::*;
use runtime_symbols::*;
Expand Down Expand Up @@ -273,6 +275,7 @@ late_lint_methods!(
InternalEqTraitMethodImpls: InternalEqTraitMethodImpls,
ImplicitProvenanceCasts: ImplicitProvenanceCasts,
CVoidReturns: CVoidReturns,
RawBorrowsViaReferences: RawBorrowsViaReferences,
]
]
);
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3016,3 +3016,29 @@ pub(crate) enum Ptr2IntSuggestion<'tcx> {
cast_span: Span,
},
}

#[derive(Diagnostic)]
#[diag(
"creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers"
)]
pub(crate) struct RawBorrowViaReference<'a> {
#[subdiagnostic]
pub suggestion: RawBorrowViaReferenceSuggestion<'a>,
}

#[derive(Subdiagnostic)]
pub(crate) enum RawBorrowViaReferenceSuggestion<'a> {
#[multipart_suggestion(
"consider using `&raw {$mutbl}` for a safer and more explicit raw pointer",
applicability = "machine-applicable"
)]
Spanful {
#[suggestion_part(code = "&raw {mutbl} ")]
left: Span,
#[suggestion_part(code = "")]
right: Span,
mutbl: &'a str,
},
#[help("consider using `&raw {$mutbl}` for a safer and more explicit raw pointer")]
Spanless { mutbl: &'a str },
}
83 changes: 83 additions & 0 deletions compiler/rustc_lint/src/raw_borrows_via_references.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use rustc_ast::BorrowKind;
use rustc_hir::{Expr, ExprKind, TyKind};
use rustc_session::{declare_lint, declare_lint_pass};

use crate::lints::{RawBorrowViaReference, RawBorrowViaReferenceSuggestion};
use crate::{LateContext, LateLintPass, LintContext};

declare_lint! {
/// The `raw_borrows_via_references` lint checks for references that decay immediately into raw borrows.
///
/// ### Example
///
/// ```rust
/// #![warn(raw_borrows_via_references)]
///
/// fn via_ref(x: *const (i32, i32)) -> *const i32 {
/// unsafe { &(*x).0 as *const i32 }
/// }
///
/// fn main() {
/// let x = (0, 1);
/// let _r = via_ref(&x);
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Creating unnecessary references is discouraged because it makes code
/// less explicit and can lead to undefined behavior. Creating a reference
/// induces aliasing assumptions that the compiler relies on, so an
/// otherwise-pointless reference can cause undefined behavior even when the
/// reference is never read through. Avoiding them keeps the code more
/// explicit and easier to reason about.
///
/// See the [Reference] for the full set of validity requirements that
/// references must uphold.
///
/// [Reference]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
///
/// This lint is "allow" by default because it will trigger for a large
/// amount of existing Rust code.
/// Eventually it is desired for this to become warn-by-default.
pub RAW_BORROWS_VIA_REFERENCES,
// FIXME: This should eventually be `Warn`, see the "Explanation" above.
Allow,
"creating raw borrows via references is discouraged"
}

declare_lint_pass!(RawBorrowsViaReferences => [RAW_BORROWS_VIA_REFERENCES]);

impl<'tcx> LateLintPass<'tcx> for RawBorrowsViaReferences {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
if let ExprKind::Cast(exp, ty) = expr.kind
&& let ExprKind::AddrOf(BorrowKind::Ref, mutbl, addr_of_exp) = exp.kind
&& let TyKind::Ptr(_) = ty.kind
&& addr_of_exp.is_syntactic_place_expr()
{
let suggestion = if let Some(addr_of_span) =
addr_of_exp.span.find_ancestor_in_same_ctxt(expr.span)
&& let Some(ty_span) = ty.span.find_ancestor_in_same_ctxt(expr.span)
&& expr.span.can_be_used_for_suggestions()
&& addr_of_span.can_be_used_for_suggestions()
&& ty_span.can_be_used_for_suggestions()
{
RawBorrowViaReferenceSuggestion::Spanful {
left: expr.span.until(addr_of_span),
right: addr_of_span.shrink_to_hi().until(ty_span.shrink_to_hi()),
mutbl: mutbl.ptr_str(),
}
} else {
RawBorrowViaReferenceSuggestion::Spanless { mutbl: mutbl.ptr_str() }
};

cx.emit_span_lint(
RAW_BORROWS_VIA_REFERENCES,
expr.span,
RawBorrowViaReference { suggestion },
);
}
}
}
Loading
Loading