Skip to content
Open
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
13 changes: 9 additions & 4 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_middle::bug;
use rustc_middle::mir::interpret::{
InterpErrorKind, InvalidMetaKind, Misalignment, Provenance, alloc_range, interp_ok,
InterpErrorKind, InvalidMetaKind, Misalignment, PointerArithmetic, Provenance, alloc_range,
interp_ok,
};
use rustc_middle::ty::layout::{LayoutCx, TyAndLayout};
use rustc_middle::ty::{self, Ty};
Expand Down Expand Up @@ -653,9 +654,13 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
let scalar = Scalar::from_maybe_pointer(place.ptr(), self.ecx);
// Skip this if we don't know the absolute address (during CTFE).
if let Ok(addr) = scalar.try_to_scalar_int() {
// Try to compute the end address.
let addr = Size::from_bytes(addr.to_target_usize(*self.ecx.tcx));
if addr.checked_add(size, self.ecx).is_none() {
// Try to compute the end address. Cannot use `Size` addition as that also applies
// the "max obj size" bound.
let addr = Size::from_bytes(addr.to_target_usize(*self.ecx.tcx)).bytes();
if addr
.checked_add(size.bytes())
.is_none_or(|result| result >= self.ecx.target_usize_max())
{
throw_validation_failure!(
self.path,
format!(
Expand Down
25 changes: 12 additions & 13 deletions compiler/rustc_resolve/src/effective_visibilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,31 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
let Some(decl) = name_resolution.borrow(self.r).best_decl() else {
continue;
};
self.update_decl_chain(decl, ParentId::Def(module_id));
self.update_decl_chain(decl, ParentId::Def(module_id), &mut FxHashSet::default());
}
}

/// Update effective visibilities for the whole reexport chain of a declaration.
/// Set the given effective visibility level to `Level::Direct` and
/// sets the rest of the `use` chain to `Level::Reexported` until
/// we hit the actual exported item.
fn update_decl_chain(&mut self, mut decl: Decl<'ra>, mut parent_id: ParentId<'ra>) {
fn update_decl_chain(
&mut self,
mut decl: Decl<'ra>,
mut parent_id: ParentId<'ra>,
seen_most_visible: &mut FxHashSet<Decl<'ra>>,
) {
let priv_vis = |this: &Self, parent_id, decl| match parent_id {
ParentId::Def(_) => this.current_private_vis,
ParentId::Import(_) => this.r.private_vis_decl(decl),
};
while let DeclKind::Import { source_decl, .. } = decl.kind {
self.update_import(decl, parent_id, priv_vis(self, parent_id, decl));
if let Some(max_vis_decl) = decl.ambiguity_vis_max.get() {
// The name is exported with the visibility of the most visible declaration
// in its ambiguous glob set (see `DeclData::vis`), so everything on that
// declaration's reexport chain, including the final item, must get its
// effective visibility from that declaration as well. Otherwise the item
// would be considered unreachable by dead code analysis and metadata
// encoding despite being exported (see the regression test
// `ambiguous-import-visibility-globglob-mir.rs`).
// This also avoids the most visible import in an ambiguous glob set
// being reported as unused.
self.update_decl_chain(max_vis_decl, parent_id);
// `ambiguity_vis_max` can cycle on mutual globs; follow each once.
if let Some(most_visible) = decl.ambiguity_vis_max.get()
&& seen_most_visible.insert(most_visible)
{
self.update_decl_chain(most_visible, parent_id, seen_most_visible);
}
parent_id = ParentId::Import(decl);
decl = source_decl;
Expand Down
1,037 changes: 445 additions & 592 deletions src/stage0

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/tools/miri/tests/pass/both_borrows/maybe_dangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
boxy();
reference();
write_through_shared_ref();
large();
}

fn boxy() {
Expand Down Expand Up @@ -58,3 +59,8 @@ fn write_through_shared_ref() {
}
}
}

fn large() {
// Used to be rejected due to faulty logic for the "does this fit the address space" check.
let _x: MaybeDangling<&i8> = unsafe { mem::transmute(usize::MAX - 127) };
}
30 changes: 30 additions & 0 deletions tests/ui/imports/ambiguous-import-visibility-globglob-cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// issue: rust-lang/rust#160685
// Mutual globs of the same item cycle through `ambiguity_vis_max`.

#![feature(rustc_attrs)]
#![allow(internal_features)]
#![deny(dead_code)]

pub mod axiomatic {
use super::*; // not pub
pub use self::own::*;

pub mod own {
pub use super::*;
pub use super::orphan::*;
}

pub mod orphan {
pub use super::private::CollectionDescriptor;
}

mod private {
#[rustc_effective_visibility]
pub struct CollectionDescriptor {}
//~^ ERROR Direct: pub(in crate::axiomatic), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub
}
}

pub use axiomatic::orphan::*;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Direct: pub(in crate::axiomatic), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub
--> $DIR/ambiguous-import-visibility-globglob-cycle.rs:23:9
|
LL | pub struct CollectionDescriptor {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

11 changes: 3 additions & 8 deletions tests/ui/imports/ambiguous-import-visibility-globglob-mir.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Regression test for the 1.96 -> 1.97 stable-to-stable regression: an item exported
// only through a public glob, and also glob-imported with restricted visibility through
// a private facade, lost its exported effective visibility. The defining crate then
// skipped encoding its optimized MIR (and warned dead_code) while name resolution still
// exported the item and it remained `cross_crate_inlinable`, so downstream crates failed
// with "missing optimized MIR". This test pins the missing-MIR half; the dead_code half
// is checked by the sibling test `ambiguous-import-visibility-globglob-reachable.rs`
// (via its `#![deny(dead_code)]`).
// issue: rust-lang/rust#159038
// Downstream missing optimized MIR when a restricted glob wins the slot.
// Dead code: `ambiguous-import-visibility-globglob-reachable.rs`.

//@ build-pass
//@ aux-build:ambiguous-import-visibility-globglob-mir.rs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Regression test for the 1.96 -> 1.97 stable-to-stable regression: an item exported
// only through a public glob, and also glob-imported with restricted visibility through
// a private facade, lost its exported effective visibility while name resolution still
// exported it. Downstream: spurious dead_code in this crate, "missing optimized MIR" in
// dependent crates (see ambiguous-import-visibility-globglob-mir.rs). The public glob
// declaration must drive the effective visibility of the whole reexport chain.
// issue: rust-lang/rust#159038
// A restricted glob in the slot must not hide a more public glob of the same item.

#![feature(rustc_attrs)]
#![allow(internal_features)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub
--> $DIR/ambiguous-import-visibility-globglob-reachable.rs:14:5
--> $DIR/ambiguous-import-visibility-globglob-reachable.rs:10:5
|
LL | pub fn f() {}
| ^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// An item exported only through a public glob, while also glob-imported into the
// same module through a facade with restricted visibility. The restricted duplicate
// must not make `f` unreachable: its optimized MIR must still be encoded for
// downstream crates (it is `cross_crate_inlinable`).
// Restricted glob must not stop `f` from being encoded.

mod inner {
pub fn f() -> u32 {
Expand Down
Loading