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
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4053,7 +4053,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {

fn explain_deref_coercion(&mut self, loan: &BorrowData<'tcx>, err: &mut Diag<'_>) {
let tcx = self.infcx.tcx;
if let Some(Terminator { kind: TerminatorKind::Call { call_source, fn_span, .. }, .. }) =
if let Terminator { kind: TerminatorKind::Call { call_source, fn_span, .. }, .. } =
&self.body[loan.reserve_location.block].terminator
&& let Some((method_did, method_args)) = mir::find_self_call(
tcx,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/find_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a, 'tcx> UseFinder<'a, 'tcx> {
if is_statement {
visitor.visit_statement(&block_data.statements[p.statement_index], p);
} else {
visitor.visit_terminator(block_data.terminator.as_ref().unwrap(), p);
visitor.visit_terminator(&block_data.terminator, p);
}

match visitor.def_use_result {
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,15 +556,15 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
);
if !is_terminator {
continue;
} else if let Some(Terminator {
} else if let Terminator {
kind:
TerminatorKind::Call {
func,
call_source: CallSource::OverloadedOperator,
..
},
..
}) = &bbd.terminator
} = &bbd.terminator
{
if let Some(source) =
BorrowedContentSource::from_call(func.ty(self.body, tcx), tcx)
Expand Down Expand Up @@ -1071,9 +1071,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {

debug!("move_spans: target_temp = {:?}", target_temp);

if let Some(Terminator {
kind: TerminatorKind::Call { fn_span, call_source, .. }, ..
}) = &self.body[location.block].terminator
if let Terminator { kind: TerminatorKind::Call { fn_span, call_source, .. }, .. } =
&self.body[location.block].terminator
{
let Some((method_did, method_args)) =
find_self_call(self.infcx.tcx, self.body, target_temp, location.block)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/region_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ impl<'tcx> RegionInferenceContextInner<'tcx> {
for location in locations {
if from_location.is_predecessor_of(location, body) {
let bb = &body[location.block];
if let Some(terminator) = &bb.terminator
if let terminator = &bb.terminator
// terminator of a loop should be TerminatorKind::FalseUnwind
&& let TerminatorKind::FalseUnwind { .. } = terminator.kind
{
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_gcc/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ fn recursively_inline<'gcc, 'tcx>(
// `expect_local` ought to never fail: we should be checking a function within this codegen unit.
let body = cx.tcx.optimized_mir(instance.def_id());
for block in body.basic_blocks.iter() {
let Some(ref terminator) = block.terminator else { continue };
// I assume that the recursive-inline issue applies only to functions, and not to drops.
// In principle, a recursive, `#[inline(always)]` drop could(?) exist, but I don't think it does.
let TerminatorKind::Call { ref func, .. } = terminator.kind else { continue };
let TerminatorKind::Call { ref func, .. } = block.terminator.kind else { continue };
let Some((def, _args)) = func.const_fn_def() else { continue };
// Check if the called function is recursively inline.
if matches!(
Expand Down
20 changes: 13 additions & 7 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use rustc_ast_pretty::pprust::expr_to_string;
use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::{AttributeKind, DocAttribute, find_attr};
use rustc_attr_parsing::AttributeParser;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, Diagnostic, msg};
use rustc_feature::GateIssue;
use rustc_hir::def::{DefKind, Res};
Expand All @@ -40,12 +39,14 @@ use rustc_middle::ty::{
self, AssocContainer, Ty, TyCtxt, TypeVisitableExt, Unnormalized, Upcast, VariantDef,
};
use rustc_span::edition::Edition;
use rustc_span::hygiene::SyntaxContext;
use rustc_span::{DUMMY_SP, Ident, InnerSpan, Span, Spanned, Symbol, bug, kw, sym};
use rustc_target::asm::InlineAsmArch;
use rustc_trait_selection::infer::{InferCtxtExt, TyCtxtInferExt};
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::misc::type_allowed_to_implement_copy;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use smallvec::SmallVec;

use crate::diagnostics::{
BuiltinAnonymousParams, BuiltinConstNoMangle, BuiltinDerefNullptr, BuiltinDoubleNegations,
Expand Down Expand Up @@ -772,24 +773,29 @@ declare_lint_pass!(
);

fn find_duplicated_parameters(cx: &EarlyContext<'_>, params: &[ast::Param]) {
let mut map = FxHashMap::default();
if params.len() < 2 {
return;
}

// 16 bytes per entry (Symbol: 4B, SyntaxContext: 4B, Span: 8B) -> 128B on stack
let mut seen: SmallVec<[(Symbol, SyntaxContext, Span); 8]> = SmallVec::new();

for param in params {
if let ast::PatKind::Ident(_, ident, _) = param.pat.kind {
if param.is_self() {
continue;
}

let name = ident.normalize_to_macro_rules();

if let Some(&prev_use) = map.get(&name) {
let ctxt = ident.span.ctxt().normalize_to_macro_rules();
if let Some(prev_use) = seen.iter().find_map(|&(name, prev_ctxt, span)| {
(name == ident.name && prev_ctxt == ctxt).then_some(span)
}) {
cx.emit_span_lint(
DUPLICATE_PARAMETER_NAMES,
ident.span,
BuiltinDuplicateParameterNames { span: ident.span, ident, prev_use },
);
} else {
map.insert(name, ident.span);
seen.push((ident.name, ctxt, ident.span));
}
}
}
Expand Down
30 changes: 4 additions & 26 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ pub struct BasicBlockData<'tcx> {
/// exception is that certain passes, such as `simplify_cfg`, swap
/// out the terminator temporarily with `None` while they continue
/// to recurse over the set of basic blocks.
pub terminator: Option<Terminator<'tcx>>,
pub terminator: Terminator<'tcx>,

/// If true, this block lies on an unwind path. This is used
/// during codegen where distinct kinds of basic blocks may be
Expand All @@ -1349,28 +1349,6 @@ impl<'tcx> BasicBlockData<'tcx> {
statements: Vec<Statement<'tcx>>,
terminator: Terminator<'tcx>,
is_cleanup: bool,
) -> BasicBlockData<'tcx> {
BasicBlockData {
statements,
after_last_stmt_debuginfos: StmtDebugInfos::default(),
terminator: Some(terminator),
is_cleanup,
}
}

/// This is being phased out. Use [`BasicBlockData::new`] instead.
pub fn maybe_unterminated(
terminator: Option<Terminator<'tcx>>,
is_cleanup: bool,
) -> BasicBlockData<'tcx> {
BasicBlockData::maybe_unterminated_stmts(Vec::new(), terminator, is_cleanup)
}

/// This is being phased out. Use [`BasicBlockData::new_stmts`] instead.
pub fn maybe_unterminated_stmts(
statements: Vec<Statement<'tcx>>,
terminator: Option<Terminator<'tcx>>,
is_cleanup: bool,
) -> BasicBlockData<'tcx> {
BasicBlockData {
statements,
Expand All @@ -1386,12 +1364,12 @@ impl<'tcx> BasicBlockData<'tcx> {
/// provides a convenient way to reach the terminator.
#[inline]
pub fn terminator(&self) -> &Terminator<'tcx> {
self.terminator.as_ref().expect("invalid terminator state")
&self.terminator
}

#[inline]
pub fn terminator_mut(&mut self) -> &mut Terminator<'tcx> {
self.terminator.as_mut().expect("invalid terminator state")
&mut self.terminator
}

/// Does the block have no statements and an unreachable terminator?
Expand Down Expand Up @@ -1713,7 +1691,7 @@ pub fn find_self_call<'tcx>(
block: BasicBlock,
) -> Option<(DefId, GenericArgs<'tcx>)> {
debug!("find_self_call(local={:?}): terminator={:?}", local, body[block].terminator);
if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) =
if let Terminator { kind: TerminatorKind::Call { func, args, .. }, .. } =
&body[block].terminator
&& let Operand::Constant(ConstOperand { const_, .. }) = func
&& let ty::FnDef(def_id, fn_args) = *const_.ty().kind()
Expand Down
44 changes: 21 additions & 23 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,33 +837,31 @@ impl<'a, 'tcx> MirWriter<'a, 'tcx> {

// Terminator at the bottom.
(self.extra_data)(PassWhere::BeforeLocation(current_location), w)?;
if data.terminator.is_some() {
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
if self.options.include_extra_comments {
writeln!(
w,
"{:A$} // {}{}",
indented_terminator,
if self.tcx.sess.verbose_internals() {
format!("{current_location:?}: ")
} else {
String::new()
},
comment(self.tcx, data.terminator().source_info),
A = ALIGN,
)?;
} else {
writeln!(w, "{indented_terminator}")?;
}

write_extra(
self.tcx,
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
if self.options.include_extra_comments {
writeln!(
w,
&|visitor| visitor.visit_terminator(data.terminator(), current_location),
self.options,
"{:A$} // {}{}",
indented_terminator,
if self.tcx.sess.verbose_internals() {
format!("{current_location:?}: ")
} else {
String::new()
},
comment(self.tcx, data.terminator().source_info),
A = ALIGN,
)?;
} else {
writeln!(w, "{indented_terminator}")?;
}

write_extra(
self.tcx,
w,
&|visitor| visitor.visit_terminator(data.terminator(), current_location),
self.options,
)?;

(self.extra_data)(PassWhere::AfterLocation(current_location), w)?;
(self.extra_data)(PassWhere::AfterTerminator(block), w)?;

Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/mir/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {

let data = &self.body[idx];

if let Some(ref term) = data.terminator {
self.worklist.extend(term.successors());
}
self.worklist.extend(data.terminator.successors());

return Some((idx, data));
}
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,7 @@ macro_rules! make_mir_visitor {
for debuginfo in after_last_stmt_debuginfos as & $($mutability)? [_] {
self.visit_statement_debuginfo(debuginfo, location);
}
if let Some(terminator) = terminator {
self.visit_terminator(terminator, location);
}
self.visit_terminator(terminator, location);
}

fn super_source_scope_data(
Expand Down Expand Up @@ -998,9 +996,7 @@ macro_rules! make_mir_visitor {
let basic_block =
& $($mutability)? basic_blocks!(body, $($mutability, true)?)[location.block];
if basic_block.statements.len() == location.statement_index {
if let Some(ref $($mutability)? terminator) = basic_block.terminator {
self.visit_terminator(terminator, location)
}
self.visit_terminator( & $($mutability)? basic_block.terminator, location)
} else {
let statement = & $($mutability)?
basic_block.statements[location.statement_index];
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_mir_build/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
);
body.coverage_early_info = self.coverage_info.map(|b| b.into_done());

let writer = pretty::MirWriter::new(self.tcx);
for (index, block) in body.basic_blocks.iter().enumerate() {
if block.terminator.is_none() {
writer.write_mir_fn(&body, &mut std::io::stdout()).unwrap();
span_bug!(self.fn_span, "no terminator on block {:?}", index);
}
}

body
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/abort_unwinding_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
// This will filter to functions with `extern "C-unwind"` ABIs, for
// example.
for block in body.basic_blocks.as_mut() {
let Some(terminator) = &mut block.terminator else { continue };
let terminator = &mut block.terminator;
let span = terminator.source_info.span;

// If we see an `UnwindResume` terminator inside a function then:
Expand Down
27 changes: 25 additions & 2 deletions compiler/rustc_mir_transform/src/check_call_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,40 @@ impl<'tcx> TerminatorClassifier<'tcx> for CallRecursion<'tcx, '_> {
if args.len() != body.arg_count {
return false;
}
let caller = body.source.def_id();
let typing_env = body.typing_env(tcx);

let func_ty = func.ty(body, tcx);
if let ty::FnDef(callee, args) = *func_ty.kind() {
// Another check to avoid resolving if we can: to be recursive either the callee is the
// caller itself, or the callee is the trait method the caller implements. The second
// part of the check works because `try_resolve` can only return the trait item or an
// impl's item for that trait item.
let caller = body.source.def_id();
if callee != caller && tcx.trait_item_of(caller) != Some(callee) {
return false;
}

let typing_env = body.typing_env(tcx);
let args = args.no_bound_vars().unwrap();
let Ok(normalized_args) =
tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(args))
else {
return false;
};

// Another check to avoid resolving if we can: this time whether the args can unify.
// This is the same `relate_rigid_infer` fast reject done by both trait solvers.
// The `callee != caller` guard means the check is skipped for trait default methods.
if callee != caller {
let call_trait_args =
ty::TraitRef::from_assoc(tcx, tcx.parent(callee), normalized_args).args;
let impl_trait_args = tcx.impl_trait_ref(tcx.parent(caller)).skip_binder().args;
if !ty::DeepRejectCtxt::relate_rigid_infer(tcx)
.args_may_unify(call_trait_args, impl_trait_args)
{
return false;
}
}

let (callee, call_args) = if let Ok(Some(instance)) =
Instance::try_resolve(tcx, typing_env, callee, normalized_args)
{
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_mir_transform/src/check_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ fn insert_direct_enum_check<'tcx>(
.push(Statement::new(source_info, StatementKind::Assign(Box::new((discr_masked, rvalue)))));

// Branch based on the discriminant value.
block_data.terminator = Some(Terminator {
block_data.terminator = Terminator {
source_info,
kind: TerminatorKind::SwitchInt {
discr: Operand::Copy(discr_masked),
Expand All @@ -418,7 +418,7 @@ fn insert_direct_enum_check<'tcx>(
invalid_discr_block,
),
},
});
};
}

// Note: this function replaces the block's terminator
Expand Down Expand Up @@ -446,7 +446,7 @@ fn insert_uninhabited_enum_check<'tcx>(
))),
));

block_data.terminator = Some(Terminator {
block_data.terminator = Terminator {
source_info,
kind: TerminatorKind::Assert {
cond: Operand::Copy(is_ok),
Expand All @@ -464,7 +464,7 @@ fn insert_uninhabited_enum_check<'tcx>(
// make a failing UB check turn into much worse UB when we start unwinding.
unwind: UnwindAction::Unreachable,
},
});
};
}

// Note: this function replaces the block's terminator.
Expand Down Expand Up @@ -530,7 +530,7 @@ fn insert_niche_check<'tcx>(
))),
));

block_data.terminator = Some(Terminator {
block_data.terminator = Terminator {
source_info,
kind: TerminatorKind::Assert {
cond: Operand::Copy(is_ok),
Expand All @@ -542,5 +542,5 @@ fn insert_niche_check<'tcx>(
// make a failing UB check turn into much worse UB when we start unwinding.
unwind: UnwindAction::Unreachable,
},
});
};
}
Loading
Loading