Skip to content
Closed
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
4 changes: 2 additions & 2 deletions compiler/rustc_attr_parsing/src/attributes/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rustc_ast::tokenstream::{DelimSpan, WithTokens};
use rustc_ast::{AttrItem, Attribute, LitKind, ast, token};
use rustc_errors::{Applicability, Diagnostic, PResult, msg};
use rustc_feature::{Features, GatedCfg, find_gated_cfg};
use rustc_hir::attrs::CfgEntry;
use rustc_hir::{AttrPath, RustcVersion, Target};
use rustc_hir::attrs::{CfgEntry, RustcVersion};
use rustc_hir::{AttrPath, Target};
use rustc_parse::parser::{ForceCollect, Parser, Recovery};
use rustc_parse::{exp, parse_in};
use rustc_session::Session;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_attr_parsing/src/attributes/deprecation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_ast::LitKind;
use rustc_feature::AttributeStability;
use rustc_hir::attrs::{DeprecatedSince, Deprecation};
use rustc_hir::{RustcVersion, VERSION_PLACEHOLDER};
use rustc_hir::VERSION_PLACEHOLDER;
use rustc_hir::attrs::{DeprecatedSince, Deprecation, RustcVersion};

use super::prelude::*;
use super::util::parse_version;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr_parsing/src/attributes/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::num::IntErrorKind;
use rustc_ast::{LitKind, ast};
use rustc_data_structures::Limit;
use rustc_feature::is_builtin_attr_name;
use rustc_hir::RustcVersion;
use rustc_hir::attrs::RustcVersion;
use rustc_span::Symbol;

use crate::context::AcceptContext;
Expand Down
20 changes: 12 additions & 8 deletions compiler/rustc_borrowck/src/borrow_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,38 @@ impl<'tcx> BorrowSet<'tcx> {
}
}

// Public method to support Aquascope.
// Public method to support Aquascope and Creusot.
/// Iterate through all BorrowData in the BorrowSet.
pub fn iter(&self) -> impl Iterator<Item = &BorrowData<'tcx>> {
self.borrows.iter()
}

// The following functions are not depended upon by outside consumers.
pub(crate) fn locals_state_at_exit(&self) -> &LocalsStateAtExit {
// Public method to support Creusot.
pub fn locals_state_at_exit(&self) -> &LocalsStateAtExit {
&self.locals_state_at_exit
}

pub(crate) fn len(&self) -> usize {
// Public method to support Creusot.
pub fn len(&self) -> usize {
self.borrows.len()
}

pub(crate) fn iter_enumerated(&self) -> impl Iterator<Item = (BorrowIndex, &BorrowData<'tcx>)> {
pub fn iter_enumerated(&self) -> impl Iterator<Item = (BorrowIndex, &BorrowData<'tcx>)> {
self.borrows.iter_enumerated()
}

pub(crate) fn activations_at_location(&self, location: &Location) -> &[BorrowIndex] {
// Public method to support Creusot.
pub fn activations_at_location(&self, location: &Location) -> &[BorrowIndex] {
self.activation_map.get(&location).map_or(&[], |activations| &activations[..])
}

pub(crate) fn borrows_at_location(&self, location: &Location) -> Option<&[BorrowIndex]> {
// Public method to support Creusot.
pub fn borrows_at_location(&self, location: &Location) -> Option<&[BorrowIndex]> {
self.location_map.get(location).map(|v| v.as_slice())
}

pub(crate) fn borrows_on_local(&self, local: Local) -> Option<&IndexSet<BorrowIndex>> {
// Public method to support Creusot.
pub fn borrows_on_local(&self, local: Local) -> Option<&IndexSet<BorrowIndex>> {
self.local_map.get(&local)
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::PathBuf;
pub use ReprAttr::*;
use rustc_abi::Align;
pub use rustc_ast::attr::data_structures::*;
pub use rustc_ast::attr::version::RustcVersion;
use rustc_ast::expand::autodiff_attrs::{DiffActivity, DiffMode};
use rustc_ast::expand::typetree::TypeTree;
use rustc_ast::token::DocFragmentKind;
Expand All @@ -19,9 +20,10 @@ use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
pub use rustc_target::spec::SanitizerSet;
use thin_vec::ThinVec;

pub use crate::attrs::canonical_symbols::{CanonicalSymbol, CanonicalSymbols};
use crate::attrs::diagnostic::*;
use crate::attrs::pretty_printing::PrintAttribute;
use crate::{DefaultBodyStability, LangItem, PartialConstStability, RustcVersion, Stability};
use crate::{DefaultBodyStability, LangItem, PartialConstStability, Stability};

#[derive(Copy, Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)]
pub enum EiiImplResolution {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/attrs/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::fmt;
use std::fmt::Debug;

pub use rustc_ast::attr::data_structures::*;
use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash};
use rustc_span::{DesugaringKind, Span, Symbol, kw};
use thin_vec::ThinVec;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/attrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use data_structures::*;
pub use encode_cross_crate::EncodeCrossCrate;
pub use pretty_printing::PrintAttribute;

mod canonical_symbols;
mod data_structures;
pub mod diagnostic;
mod encode_cross_crate;
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_ast as ast;
use rustc_ast::NodeId;
use rustc_data_structures::fx::FxIndexMap;
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
use rustc_hir_id::HirId;
use rustc_macros::{Decodable, Encodable, StableHash};
use rustc_span::Symbol;
use rustc_span::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -472,7 +473,7 @@ impl DefKind {
/// pointing to the definition of `str_to_string` in the current crate.
//
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, StableHash)]
pub enum Res<Id = hir::HirId> {
pub enum Res<Id = HirId> {
/// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
///
/// **Not bound to a specific namespace.**
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rustc_data_structures::steal::Steal;
use rustc_data_structures::tagged_ptr::TaggedRef;
use rustc_data_structures::unord::UnordMap;
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
use rustc_hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
use rustc_index::IndexVec;
use rustc_macros::{Decodable, Encodable, StableHash};
use rustc_span::def_id::LocalDefId;
Expand All @@ -38,7 +39,6 @@ use tracing::debug;
use crate::attrs::AttributeKind;
use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res};
use crate::def_id::{DefId, LocalDefIdMap};
pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
use crate::intravisit::{FnKind, VisitorExt};
use crate::lints::DelayedLints;

Expand Down Expand Up @@ -5121,7 +5121,7 @@ impl<'hir> OwnerNode<'hir> {
| OwnerNode::TraitItem(TraitItem { owner_id, .. })
| OwnerNode::ImplItem(ImplItem { owner_id, .. })
| OwnerNode::ForeignItem(ForeignItem { owner_id, .. }) => *owner_id,
OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
OwnerNode::Crate(..) => rustc_hir_id::CRATE_HIR_ID.owner,
OwnerNode::Synthetic => unreachable!(),
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

use rustc_ast::Label;
use rustc_ast::visit::{VisitorResult, try_visit, visit_opt, walk_list};
use rustc_hir_id::HirId;
use rustc_span::def_id::LocalDefId;
use rustc_span::{Ident, Span, Symbol};

Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@

mod arena;
pub mod attrs;
pub mod canonical_symbols;
pub mod def;
pub mod def_path_hash_map;
pub mod definitions;
pub mod diagnostic_items;
pub use rustc_span::def_id;
mod hir;
pub use rustc_hir_id::{self as hir_id, *};
pub mod intravisit;
pub mod lang_items;
pub mod lints;
Expand All @@ -37,11 +34,11 @@ pub mod weak_lang_items;
#[cfg(test)]
mod tests;

pub use canonical_symbols::{CanonicalSymbol, CanonicalSymbols};
#[doc(no_inline)]
pub use hir::*;
pub use lang_items::{LangItem, LanguageItems};
pub use rustc_ast::attr::version::*;
pub use rustc_hir_id::*;
pub use rustc_span::def_id;
pub use stability::*;
pub use target::{MethodKind, Target};

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir/src/lints.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use rustc_data_structures::sync::{DynSend, DynSync};
use rustc_error_messages::MultiSpan;
use rustc_errors::{Diag, DiagCtxtHandle, Level};
use rustc_hir_id::HirId;
use rustc_lint_defs::LintId;

use crate::HirId;

pub type DelayedLints = Box<[DelayedLint]>;

/// During ast lowering, no lints can be emitted.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/pat_util.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::iter::Enumerate;

use rustc_hir_id::HirId;
use rustc_span::{Ident, Span};

use crate::def::{CtorOf, DefKind, Res};
use crate::def_id::{DefId, DefIdSet};
use crate::hir::{self, BindingMode, ByRef, HirId, PatKind};

use crate::hir::{self, BindingMode, ByRef, PatKind};
pub struct EnumerateAndAdjust<I> {
enumerate: Enumerate<I>,
gap_pos: usize,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/stability.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::num::NonZero;

use rustc_ast::attr::version::RustcVersion;
use rustc_macros::{BlobDecodable, Decodable, Encodable, PrintAttribute, StableHash};
use rustc_span::{ErrorGuaranteed, Symbol, sym};

use crate::RustcVersion;
use crate::attrs::PrintAttribute;

/// The version placeholder that recently stabilized features contain inside the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/dangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn lint_addr_of_local<'a>(
expr: &'a Expr<'a>,
) {
// peel casts as they do not interest us here, we want the inner expression.
let (inner, _) = super::utils::peel_casts(cx, expr);
let inner = super::utils::peel_casts(cx, expr);

if let ExprKind::AddrOf(_, _, inner_of) = inner.kind
&& let ExprKind::Path(ref qpath) = inner_of.peel_blocks().kind
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,6 @@ pub(crate) enum InvalidReferenceCastingDiag<'tcx> {
BorrowAsMut {
#[label("casting happened here")]
orig_cast: Option<Span>,
#[note(
"even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get`"
)]
ty_has_interior_mutability: bool,
},
#[diag("assigning to `&T` is undefined behavior, consider using an `UnsafeCell`")]
#[note(
Expand All @@ -1005,10 +1001,6 @@ pub(crate) enum InvalidReferenceCastingDiag<'tcx> {
AssignToRef {
#[label("casting happened here")]
orig_cast: Option<Span>,
#[note(
"even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get`"
)]
ty_has_interior_mutability: bool,
},
#[diag(
"casting references to a bigger memory layout than the backing allocation is undefined behavior, even if the reference is unused"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/ptr_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn useless_check<'a, 'tcx: 'a>(

/// Checks if the given expression is a null pointer (modulo casting)
fn is_null_ptr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Span> {
let (expr, _) = peel_casts(cx, expr);
let expr = peel_casts(cx, expr);

if let ExprKind::Call(path, []) = expr.kind
&& let ExprKind::Path(ref qpath) = path.kind
Expand Down
43 changes: 19 additions & 24 deletions compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,15 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
};

if matches!(pat, PatternKind::Borrow { mutbl: Mutability::Mut } | PatternKind::Assign)
&& let Some(ty_has_interior_mutability) =
is_cast_from_ref_to_mut_ptr(cx, init, &mut peel_casts)
&& is_invalid_cast_from_ref_to_mut_ptr(cx, init, &mut peel_casts)
{
cx.emit_span_lint(
INVALID_REFERENCE_CASTING,
expr.span,
if pat == PatternKind::Assign {
InvalidReferenceCastingDiag::AssignToRef {
orig_cast,
ty_has_interior_mutability,
}
InvalidReferenceCastingDiag::AssignToRef { orig_cast }
} else {
InvalidReferenceCastingDiag::BorrowAsMut {
orig_cast,
ty_has_interior_mutability,
}
InvalidReferenceCastingDiag::BorrowAsMut { orig_cast }
},
);
}
Expand Down Expand Up @@ -146,49 +139,51 @@ fn borrow_or_assign<'tcx>(
deref_assign_or_addr_of(e).or_else(|| ptr_write(cx, e))
}

fn is_cast_from_ref_to_mut_ptr<'tcx>(
fn is_invalid_cast_from_ref_to_mut_ptr<'tcx>(
cx: &LateContext<'tcx>,
orig_expr: &'tcx Expr<'tcx>,
mut peel_casts: impl FnMut() -> (&'tcx Expr<'tcx>, bool),
) -> Option<bool> {
mut peel_casts: impl FnMut() -> &'tcx Expr<'tcx>,
) -> bool {
let end_ty = cx.typeck_results().node_type(orig_expr.hir_id);

// Bail out early if the end type is **not** a mutable pointer.
if !matches!(end_ty.kind(), ty::RawPtr(_, Mutability::Mut)) {
return None;
return false;
}

let (e, need_check_freeze) = peel_casts();

let e = peel_casts();
let start_ty = cx.typeck_results().node_type(e.hir_id);

if let ty::Ref(_, inner_ty, Mutability::Not) = start_ty.kind() {
// If an UnsafeCell method is involved, we need to additionally check the
// inner type for the presence of the Freeze trait (ie does NOT contain
// an UnsafeCell), since in that case we would incorrectly lint on valid casts.
// We need to additionally check the inner type for the presence of the Freeze trait
// (ie does NOT contain an UnsafeCell), since in that case we would incorrectly lint
// on valid casts (see https://github.com/rust-lang/unsafe-code-guidelines/issues/281).
//
// Except on the presence of non concrete skeleton types (ie generics)
// since there is no way to make it safe for arbitrary types.
//
// However this does mean we miss out on some cases where the user doesn't go
// through an UnsafeCell but there's an UnsafeCell somewhere else in the type.
let inner_ty_has_interior_mutability =
!inner_ty.is_freeze(cx.tcx, cx.typing_env()) && inner_ty.has_concrete_skeleton();
(!need_check_freeze || !inner_ty_has_interior_mutability)
.then_some(inner_ty_has_interior_mutability)
!inner_ty_has_interior_mutability
} else {
None
false
}
}

fn is_cast_to_bigger_memory_layout<'tcx>(
cx: &LateContext<'tcx>,
orig_expr: &'tcx Expr<'tcx>,
mut peel_casts: impl FnMut() -> (&'tcx Expr<'tcx>, bool),
mut peel_casts: impl FnMut() -> &'tcx Expr<'tcx>,
) -> Option<(TyAndLayout<'tcx>, TyAndLayout<'tcx>, Expr<'tcx>)> {
let end_ty = cx.typeck_results().node_type(orig_expr.hir_id);

let ty::RawPtr(inner_end_ty, _) = end_ty.kind() else {
return None;
};

let (e, _) = peel_casts();
let e = peel_casts();
let start_ty = cx.typeck_results().node_type(e.hir_id);

let ty::Ref(_, inner_start_ty, _) = start_ty.kind() else {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/runtime_symbols.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_hir::attrs::CanonicalSymbol;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::{self as hir, CanonicalSymbol, FnSig, ForeignItemKind};
use rustc_hir::{self as hir, FnSig, ForeignItemKind};
use rustc_infer::infer::DefineOpaqueTypes;
use rustc_middle::ty::{self, Instance, PolyFnSig, Ty};
use rustc_session::{declare_lint, declare_lint_pass};
Expand Down
Loading
Loading