Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #127775

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6ff8a84
Suggest a borrow when using dbg
chenyukang Feb 12, 2024
f6c377c
offset_from intrinsic: always allow pointers to point to the same add…
RalfJung May 9, 2024
692bc34
Make parse error suggestions verbose and fix spans
estebank Jul 6, 2024
c2b3287
Make `impl` and `!` removal suggestion `short`
estebank Jul 11, 2024
dd40e0b
Tweak tests to avoid confusing suggestion output
estebank Jul 11, 2024
b5f94c6
Use more accurate span for `:` to `::` suggestion
estebank Jul 11, 2024
377d14b
More accurate incorrect use of `await` suggestion
estebank Jul 11, 2024
b6f5188
fix unused binding
estebank Jul 12, 2024
71f16bd
Make `;` suggestions inline
estebank Jul 12, 2024
90c9e32
consolidate miri-unleashed tests for mutable refs into one file
RalfJung Jul 13, 2024
dc20733
Stop using the gen keyword in the compiler
compiler-errors Jul 14, 2024
f08c43a
Suppress some fallout from gen in synstructure
compiler-errors Jul 14, 2024
80393ea
Fix trivial gen ident usage in tools
compiler-errors Jul 14, 2024
f18d4a8
Add myself to the review rotation
tgross35 Jul 14, 2024
741ed01
coverage: Store a copy of `num_bcbs` in `ExtractedMappings`
Zalathar Jul 15, 2024
d4f1f92
coverage: Restrict `ExpressionUsed` simplification to `Code` mappings
Zalathar Jul 15, 2024
d8cfe7b
Rollup merge of #120990 - chenyukang:yukang-fix-120327-dbg, r=oli-obk
matthiaskrgr Jul 15, 2024
cfc01f1
Rollup merge of #124921 - RalfJung:offset-from-same-addr, r=oli-obk
matthiaskrgr Jul 15, 2024
981a72b
Rollup merge of #127407 - estebank:parser-suggestions, r=oli-obk
matthiaskrgr Jul 15, 2024
3f653b1
Rollup merge of #127684 - RalfJung:unleashed-mutable-refs, r=oli-obk
matthiaskrgr Jul 15, 2024
e7b9260
Rollup merge of #127729 - compiler-errors:ed-2024-gen, r=oli-obk
matthiaskrgr Jul 15, 2024
499d734
Rollup merge of #127736 - tgross35:patch-1, r=Amanieu
matthiaskrgr Jul 15, 2024
dade622
Rollup merge of #127758 - Zalathar:expression-used, r=oli-obk
matthiaskrgr Jul 15, 2024
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
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ impl<'tcx> rustc_mir_dataflow::GenKillAnalysis<'tcx> for Borrows<'_, '_, 'tcx> {
panic!("could not find BorrowIndex for location {location:?}");
});

trans.gen(index);
trans.gen_(index);
}

// Make sure there are no remaining borrows for variables
Expand Down
63 changes: 61 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(rustc::untranslatable_diagnostic)]

use either::Either;
use hir::ClosureKind;
use hir::{ClosureKind, Path};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag, MultiSpan};
Expand Down Expand Up @@ -546,7 +546,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
self.suggest_cloning(err, ty, expr, None, Some(move_spans));
}
}
if let Some(pat) = finder.pat {

self.suggest_ref_for_dbg_args(expr, span, move_span, err);

// it's useless to suggest inserting `ref` when the span don't comes from local code
if let Some(pat) = finder.pat
&& !move_span.is_dummy()
&& !self.infcx.tcx.sess.source_map().is_imported(move_span)
{
*in_pattern = true;
let mut sugg = vec![(pat.span.shrink_to_lo(), "ref ".to_string())];
if let Some(pat) = finder.parent_pat {
Expand All @@ -561,6 +568,58 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
}
}

// for dbg!(x) which may take onwership, suggest dbg!(&x) instead
// but here we actually do not check whether the macro name is `dbg!`
// so that we may extend the scope a bit larger to cover more cases
fn suggest_ref_for_dbg_args(
&self,
body: &hir::Expr<'_>,
span: Option<Span>,
move_span: Span,
err: &mut Diag<'tcx>,
) {
let sm = self.infcx.tcx.sess.source_map();
let arg_code = if let Some(span) = span
&& let Ok(code) = sm.span_to_snippet(span)
{
code
} else {
return;
};
struct MatchArgFinder {
expr_span: Span,
match_arg_span: Option<Span>,
arg_code: String,
}
impl Visitor<'_> for MatchArgFinder {
fn visit_expr(&mut self, e: &hir::Expr<'_>) {
// dbg! is expanded into a match pattern, we need to find the right argument span
if let hir::ExprKind::Match(expr, ..) = &e.kind
&& let hir::ExprKind::Path(hir::QPath::Resolved(
_,
path @ Path { segments: [seg], .. },
)) = &expr.kind
&& seg.ident.name.as_str() == &self.arg_code
&& self.expr_span.source_callsite().contains(expr.span)
{
self.match_arg_span = Some(path.span);
}
hir::intravisit::walk_expr(self, e);
}
}

let mut finder = MatchArgFinder { expr_span: move_span, match_arg_span: None, arg_code };
finder.visit_expr(body);
if let Some(macro_arg_span) = finder.match_arg_span {
err.span_suggestion_verbose(
macro_arg_span.shrink_to_lo(),
"consider borrowing instead of transferring ownership",
"&",
Applicability::MachineApplicable,
);
}
}

fn report_use_of_uninitialized(
&self,
mpi: MovePathIndex,
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ impl<'tcx> FunctionCoverageCollector<'tcx> {
// For each expression ID that is directly used by one or more mappings,
// mark it as not-yet-seen. This indicates that we expect to see a
// corresponding `ExpressionUsed` statement during MIR traversal.
for term in function_coverage_info.mappings.iter().flat_map(|m| m.kind.terms()) {
if let CovTerm::Expression(id) = term {
for mapping in function_coverage_info.mappings.iter() {
// Currently we only worry about ordinary code mappings.
// For branch and MC/DC mappings, expressions might not correspond
// to any particular point in the control-flow graph.
// (Keep this in sync with the injection of `ExpressionUsed`
// statements in the `InstrumentCoverage` MIR pass.)
if let MappingKind::Code(term) = mapping.kind
&& let CovTerm::Expression(id) = term
{
expressions_seen.remove(id);
}
}
Expand Down
33 changes: 18 additions & 15 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::{
err_inval, err_ub_custom, err_unsup_format, memory::MemoryKind, throw_inval, throw_ub_custom,
throw_ub_format, util::ensure_monomorphic_enough, Allocation, CheckInAllocMsg, ConstAllocation,
GlobalId, ImmTy, InterpCx, InterpResult, MPlaceTy, Machine, OpTy, Pointer, PointerArithmetic,
Scalar,
Provenance, Scalar,
};

use crate::fluent_generated as fluent;
Expand Down Expand Up @@ -259,25 +259,28 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
// This will always return 0.
(a, b)
}
(Err(_), _) | (_, Err(_)) => {
// We managed to find a valid allocation for one pointer, but not the other.
// That means they are definitely not pointing to the same allocation.
_ if M::Provenance::OFFSET_IS_ADDR && a.addr() == b.addr() => {
// At least one of the pointers has provenance, but they also point to
// the same address so it doesn't matter; this is fine. `(0, 0)` means
// we pass all the checks below and return 0.
(0, 0)
}
// From here onwards, the pointers are definitely for different addresses
// (or we can't determine their absolute address).
(Ok((a_alloc_id, a_offset, _)), Ok((b_alloc_id, b_offset, _)))
if a_alloc_id == b_alloc_id =>
{
// Found allocation for both, and it's the same.
// Use these offsets for distance calculation.
(a_offset.bytes(), b_offset.bytes())
}
_ => {
// Not into the same allocation -- this is UB.
throw_ub_custom!(
fluent::const_eval_offset_from_different_allocations,
name = intrinsic_name,
);
}
(Ok((a_alloc_id, a_offset, _)), Ok((b_alloc_id, b_offset, _))) => {
// Found allocation for both. They must be into the same allocation.
if a_alloc_id != b_alloc_id {
throw_ub_custom!(
fluent::const_eval_offset_from_different_allocations,
name = intrinsic_name,
);
}
// Use these offsets for distance calculation.
(a_offset.bytes(), b_offset.bytes())
}
};

// Compute distance.
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3218,10 +3218,10 @@ impl<'hir> Item<'hir> {
ItemKind::Static(ty, mutbl, body), (ty, *mutbl, *body);

expect_const, (&'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
ItemKind::Const(ty, gen, body), (ty, gen, *body);
ItemKind::Const(ty, generics, body), (ty, generics, *body);

expect_fn, (&FnSig<'hir>, &'hir Generics<'hir>, BodyId),
ItemKind::Fn(sig, gen, body), (sig, gen, *body);
ItemKind::Fn(sig, generics, body), (sig, generics, *body);

expect_macro, (&ast::MacroDef, MacroKind), ItemKind::Macro(def, mk), (def, *mk);

Expand All @@ -3233,25 +3233,25 @@ impl<'hir> Item<'hir> {
expect_global_asm, &'hir InlineAsm<'hir>, ItemKind::GlobalAsm(asm), asm;

expect_ty_alias, (&'hir Ty<'hir>, &'hir Generics<'hir>),
ItemKind::TyAlias(ty, gen), (ty, gen);
ItemKind::TyAlias(ty, generics), (ty, generics);

expect_opaque_ty, &OpaqueTy<'hir>, ItemKind::OpaqueTy(ty), ty;

expect_enum, (&EnumDef<'hir>, &'hir Generics<'hir>), ItemKind::Enum(def, gen), (def, gen);
expect_enum, (&EnumDef<'hir>, &'hir Generics<'hir>), ItemKind::Enum(def, generics), (def, generics);

expect_struct, (&VariantData<'hir>, &'hir Generics<'hir>),
ItemKind::Struct(data, gen), (data, gen);
ItemKind::Struct(data, generics), (data, generics);

expect_union, (&VariantData<'hir>, &'hir Generics<'hir>),
ItemKind::Union(data, gen), (data, gen);
ItemKind::Union(data, generics), (data, generics);

expect_trait,
(IsAuto, Safety, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]),
ItemKind::Trait(is_auto, safety, gen, bounds, items),
(*is_auto, *safety, gen, bounds, items);
ItemKind::Trait(is_auto, safety, generics, bounds, items),
(*is_auto, *safety, generics, bounds, items);

expect_trait_alias, (&'hir Generics<'hir>, GenericBounds<'hir>),
ItemKind::TraitAlias(gen, bounds), (gen, bounds);
ItemKind::TraitAlias(generics, bounds), (generics, bounds);

expect_impl, &'hir Impl<'hir>, ItemKind::Impl(imp), imp;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.and_then(|node| node.generics())
.into_iter()
.flat_map(|generics| generics.params)
.find(|gen| &gen.def_id.to_def_id() == res_def_id)
.find(|param| &param.def_id.to_def_id() == res_def_id)
} else {
None
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_macros/src/diagnostics/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl<'a> DiagnosticDerive<'a> {
});

// A lifetime of `'a` causes conflicts, but `_sess` is fine.
// FIXME(edition_2024): Fix the `keyword_idents_2024` lint to not trigger here?
#[allow(keyword_idents_2024)]
let mut imp = structure.gen_impl(quote! {
gen impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for @Self
where G: rustc_errors::EmissionGuarantee
Expand Down Expand Up @@ -148,6 +150,8 @@ impl<'a> LintDiagnosticDerive<'a> {
}
});

// FIXME(edition_2024): Fix the `keyword_idents_2024` lint to not trigger here?
#[allow(keyword_idents_2024)]
let mut imp = structure.gen_impl(quote! {
gen impl<'__a> rustc_errors::LintDiagnostic<'__a, ()> for @Self {
#[track_caller]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_macros/src/diagnostics/subdiagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ impl SubdiagnosticDerive {

let diag = &self.diag;
let f = &self.f;

// FIXME(edition_2024): Fix the `keyword_idents_2024` lint to not trigger here?
#[allow(keyword_idents_2024)]
let ret = structure.gen_impl(quote! {
gen impl rustc_errors::Subdiagnostic for @Self {
fn add_to_diag_with<__G, __F>(
Expand All @@ -100,6 +103,7 @@ impl SubdiagnosticDerive {
}
}
});

ret
}
}
Expand Down
13 changes: 0 additions & 13 deletions compiler/rustc_middle/src/mir/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,6 @@ pub enum MappingKind {
}

impl MappingKind {
/// Iterator over all coverage terms in this mapping kind.
pub fn terms(&self) -> impl Iterator<Item = CovTerm> {
let zero = || None.into_iter().chain(None);
let one = |a| Some(a).into_iter().chain(None);
let two = |a, b| Some(a).into_iter().chain(Some(b));
match *self {
Self::Code(term) => one(term),
Self::Branch { true_term, false_term } => two(true_term, false_term),
Self::MCDCBranch { true_term, false_term, .. } => two(true_term, false_term),
Self::MCDCDecision(_) => zero(),
}
}

/// Returns a copy of this mapping kind, in which all coverage terms have
/// been replaced with ones returned by the given function.
pub fn map_terms(&self, map_fn: impl Fn(CovTerm) -> CovTerm) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,14 @@ macro_rules! extra_body_methods {
macro_rules! super_body {
($self:ident, $body:ident, $($mutability:ident, $invalidate:tt)?) => {
let span = $body.span;
if let Some(gen) = &$($mutability)? $body.coroutine {
if let Some(yield_ty) = $(& $mutability)? gen.yield_ty {
if let Some(coroutine) = &$($mutability)? $body.coroutine {
if let Some(yield_ty) = $(& $mutability)? coroutine.yield_ty {
$self.visit_ty(
yield_ty,
TyContext::YieldTy(SourceInfo::outermost(span))
);
}
if let Some(resume_ty) = $(& $mutability)? gen.resume_ty {
if let Some(resume_ty) = $(& $mutability)? coroutine.resume_ty {
$self.visit_ty(
resume_ty,
TyContext::ResumeTy(SourceInfo::outermost(span))
Expand Down
30 changes: 15 additions & 15 deletions compiler/rustc_mir_dataflow/src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,15 @@ where
/// building up a `GenKillSet` and then throwing it away.
pub trait GenKill<T> {
/// Inserts `elem` into the state vector.
fn gen(&mut self, elem: T);
fn gen_(&mut self, elem: T);

/// Removes `elem` from the state vector.
fn kill(&mut self, elem: T);

/// Calls `gen` for each element in `elems`.
fn gen_all(&mut self, elems: impl IntoIterator<Item = T>) {
for elem in elems {
self.gen(elem);
self.gen_(elem);
}
}

Expand All @@ -424,44 +424,44 @@ pub trait GenKill<T> {

/// Stores a transfer function for a gen/kill problem.
///
/// Calling `gen`/`kill` on a `GenKillSet` will "build up" a transfer function so that it can be
/// applied multiple times efficiently. When there are multiple calls to `gen` and/or `kill` for
/// Calling `gen_`/`kill` on a `GenKillSet` will "build up" a transfer function so that it can be
/// applied multiple times efficiently. When there are multiple calls to `gen_` and/or `kill` for
/// the same element, the most recent one takes precedence.
#[derive(Clone)]
pub struct GenKillSet<T> {
gen: HybridBitSet<T>,
gen_: HybridBitSet<T>,
kill: HybridBitSet<T>,
}

impl<T: Idx> GenKillSet<T> {
/// Creates a new transfer function that will leave the dataflow state unchanged.
pub fn identity(universe: usize) -> Self {
GenKillSet {
gen: HybridBitSet::new_empty(universe),
gen_: HybridBitSet::new_empty(universe),
kill: HybridBitSet::new_empty(universe),
}
}

pub fn apply(&self, state: &mut impl BitSetExt<T>) {
state.union(&self.gen);
state.union(&self.gen_);
state.subtract(&self.kill);
}
}

impl<T: Idx> GenKill<T> for GenKillSet<T> {
fn gen(&mut self, elem: T) {
self.gen.insert(elem);
fn gen_(&mut self, elem: T) {
self.gen_.insert(elem);
self.kill.remove(elem);
}

fn kill(&mut self, elem: T) {
self.kill.insert(elem);
self.gen.remove(elem);
self.gen_.remove(elem);
}
}

impl<T: Idx> GenKill<T> for BitSet<T> {
fn gen(&mut self, elem: T) {
fn gen_(&mut self, elem: T) {
self.insert(elem);
}

Expand All @@ -471,7 +471,7 @@ impl<T: Idx> GenKill<T> for BitSet<T> {
}

impl<T: Idx> GenKill<T> for ChunkedBitSet<T> {
fn gen(&mut self, elem: T) {
fn gen_(&mut self, elem: T) {
self.insert(elem);
}

Expand All @@ -481,11 +481,11 @@ impl<T: Idx> GenKill<T> for ChunkedBitSet<T> {
}

impl<T, S: GenKill<T>> GenKill<T> for MaybeReachable<S> {
fn gen(&mut self, elem: T) {
fn gen_(&mut self, elem: T) {
match self {
// If the state is not reachable, adding an element does nothing.
MaybeReachable::Unreachable => {}
MaybeReachable::Reachable(set) => set.gen(elem),
MaybeReachable::Reachable(set) => set.gen_(elem),
}
}

Expand All @@ -499,7 +499,7 @@ impl<T, S: GenKill<T>> GenKill<T> for MaybeReachable<S> {
}

impl<T: Idx> GenKill<T> for lattice::Dual<BitSet<T>> {
fn gen(&mut self, elem: T) {
fn gen_(&mut self, elem: T) {
self.0.insert(elem);
}

Expand Down
Loading
Loading