Skip to content

Commit 461de74

Browse files
committed
Auto merge of rust-lang#136980 - cuviper:beta-next, r=cuviper
[beta] backports - Pattern Migration 2024: try to suggest eliding redundant binding modifiers rust-lang#136577, rust-lang#136857 - chore: update rustc-hash 2.1.0 to 2.1.1 rust-lang#136605 - Make `AsyncFnOnce`, `AsyncFnMut`, `AsyncFn` non-`#[fundamental]` rust-lang#136724 - fix ensure_monomorphic_enough rust-lang#136839 - Revert "Stabilize `extended_varargs_abi_support`" rust-lang#136897, rust-lang#136934 r? cuviper
2 parents 3821385 + e5ba20b commit 461de74

32 files changed

+1077
-255
lines changed

Cargo.lock

+8-8
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ dependencies = [
19831983
"anyhow",
19841984
"clap",
19851985
"fs-err",
1986-
"rustc-hash 2.1.0",
1986+
"rustc-hash 2.1.1",
19871987
"rustdoc-json-types",
19881988
"serde",
19891989
"serde_json",
@@ -3152,7 +3152,7 @@ dependencies = [
31523152
"proc-macro2",
31533153
"quote",
31543154
"rinja_parser",
3155-
"rustc-hash 2.1.0",
3155+
"rustc-hash 2.1.1",
31563156
"serde",
31573157
"syn 2.0.93",
31583158
]
@@ -3216,9 +3216,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
32163216

32173217
[[package]]
32183218
name = "rustc-hash"
3219-
version = "2.1.0"
3219+
version = "2.1.1"
32203220
source = "registry+https://github.com/rust-lang/crates.io-index"
3221-
checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497"
3221+
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
32223222

32233223
[[package]]
32243224
name = "rustc-main"
@@ -3620,7 +3620,7 @@ dependencies = [
36203620
"memmap2",
36213621
"parking_lot",
36223622
"portable-atomic",
3623-
"rustc-hash 2.1.0",
3623+
"rustc-hash 2.1.1",
36243624
"rustc-rayon",
36253625
"rustc-stable-hash",
36263626
"rustc_arena",
@@ -4323,7 +4323,7 @@ dependencies = [
43234323
name = "rustc_pattern_analysis"
43244324
version = "0.0.0"
43254325
dependencies = [
4326-
"rustc-hash 2.1.0",
4326+
"rustc-hash 2.1.1",
43274327
"rustc_abi",
43284328
"rustc_apfloat",
43294329
"rustc_arena",
@@ -4719,7 +4719,7 @@ name = "rustdoc-json-types"
47194719
version = "0.1.0"
47204720
dependencies = [
47214721
"bincode",
4722-
"rustc-hash 2.1.0",
4722+
"rustc-hash 2.1.1",
47234723
"serde",
47244724
"serde_json",
47254725
]
@@ -5363,7 +5363,7 @@ dependencies = [
53635363
"ignore",
53645364
"miropt-test-tools",
53655365
"regex",
5366-
"rustc-hash 2.1.0",
5366+
"rustc-hash 2.1.1",
53675367
"semver",
53685368
"similar",
53695369
"termcolor",

compiler/rustc_const_eval/src/interpret/util.rs

+3-41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use std::ops::ControlFlow;
2-
31
use rustc_hir::def_id::LocalDefId;
42
use rustc_middle::mir;
53
use rustc_middle::mir::interpret::{Allocation, InterpResult, Pointer};
64
use rustc_middle::ty::layout::TyAndLayout;
7-
use rustc_middle::ty::{
8-
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
9-
};
5+
use rustc_middle::ty::{TyCtxt, TypeVisitable, TypeVisitableExt};
106
use tracing::debug;
117

128
use super::{InterpCx, MPlaceTy, MemoryKind, interp_ok, throw_inval};
@@ -20,44 +16,10 @@ where
2016
T: TypeVisitable<TyCtxt<'tcx>>,
2117
{
2218
debug!("ensure_monomorphic_enough: ty={:?}", ty);
23-
if !ty.has_param() {
24-
return interp_ok(());
25-
}
26-
27-
struct FoundParam;
28-
struct UsedParamsNeedInstantiationVisitor {}
29-
30-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor {
31-
type Result = ControlFlow<FoundParam>;
32-
33-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
34-
if !ty.has_param() {
35-
return ControlFlow::Continue(());
36-
}
37-
38-
match *ty.kind() {
39-
ty::Param(_) => ControlFlow::Break(FoundParam),
40-
ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..) | ty::FnDef(..) => {
41-
ControlFlow::Continue(())
42-
}
43-
_ => ty.super_visit_with(self),
44-
}
45-
}
46-
47-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
48-
match c.kind() {
49-
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
50-
_ => c.super_visit_with(self),
51-
}
52-
}
53-
}
54-
55-
let mut vis = UsedParamsNeedInstantiationVisitor {};
56-
if matches!(ty.visit_with(&mut vis), ControlFlow::Break(FoundParam)) {
19+
if ty.has_param() {
5720
throw_inval!(TooGeneric);
58-
} else {
59-
interp_ok(())
6021
}
22+
interp_ok(())
6123
}
6224

6325
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {

compiler/rustc_feature/src/accepted.rs

-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ declare_features! (
197197
(accepted, expr_fragment_specifier_2024, "1.83.0", Some(123742)),
198198
/// Allows arbitrary expressions in key-value attributes at parse time.
199199
(accepted, extended_key_value_attributes, "1.54.0", Some(78835)),
200-
/// Allows using `efiapi`, `aapcs`, `sysv64` and `win64` as calling
201-
/// convention for functions with varargs.
202-
(accepted, extended_varargs_abi_support, "1.85.0", Some(100189)),
203200
/// Allows resolving absolute paths as paths from other crates.
204201
(accepted, extern_absolute_paths, "1.30.0", Some(44660)),
205202
/// Allows `extern crate foo as bar;`. This puts `bar` into extern prelude.

compiler/rustc_feature/src/unstable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ declare_features! (
479479
(unstable, exhaustive_patterns, "1.13.0", Some(51085)),
480480
/// Allows explicit tail calls via `become` expression.
481481
(incomplete, explicit_tail_calls, "1.72.0", Some(112788)),
482+
/// Allows using `efiapi`, `sysv64` and `win64` as calling convention
483+
/// for functions with varargs.
484+
(unstable, extended_varargs_abi_support, "1.65.0", Some(100189)),
482485
/// Allows defining `extern type`s.
483486
(unstable, extern_types, "1.23.0", Some(43467)),
484487
/// Allow using 128-bit (quad precision) floating point numbers.

compiler/rustc_hir_analysis/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ hir_analysis_value_of_associated_struct_already_specified =
602602
.label = re-bound here
603603
.previous_bound_label = `{$item_name}` bound here first
604604
605-
hir_analysis_variadic_function_compatible_convention = C-variadic function must have a compatible calling convention, like `C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`
605+
hir_analysis_variadic_function_compatible_convention = C-variadic function must have a compatible calling convention, like {$conventions}
606606
.label = C-variadic function must have a compatible calling convention
607607
608608
hir_analysis_variances_of = {$variances}

compiler/rustc_hir_analysis/src/errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,11 @@ pub(crate) struct MainFunctionGenericParameters {
688688

689689
#[derive(Diagnostic)]
690690
#[diag(hir_analysis_variadic_function_compatible_convention, code = E0045)]
691-
pub(crate) struct VariadicFunctionCompatibleConvention {
691+
pub(crate) struct VariadicFunctionCompatibleConvention<'a> {
692692
#[primary_span]
693693
#[label]
694694
pub span: Span,
695+
pub conventions: &'a str,
695696
}
696697

697698
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/lib.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ use rustc_middle::middle;
100100
use rustc_middle::mir::interpret::GlobalId;
101101
use rustc_middle::query::Providers;
102102
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
103+
use rustc_session::parse::feature_err;
104+
use rustc_span::symbol::sym;
103105
use rustc_span::Span;
104106
use rustc_trait_selection::traits;
105107

@@ -113,9 +115,34 @@ fn require_c_abi_if_c_variadic(
113115
abi: ExternAbi,
114116
span: Span,
115117
) {
116-
if decl.c_variadic && !abi.supports_varargs() {
117-
tcx.dcx().emit_err(errors::VariadicFunctionCompatibleConvention { span });
118+
const CONVENTIONS_UNSTABLE: &str =
119+
"`C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`";
120+
const CONVENTIONS_STABLE: &str = "`C` or `cdecl`";
121+
const UNSTABLE_EXPLAIN: &str =
122+
"using calling conventions other than `C` or `cdecl` for varargs functions is unstable";
123+
124+
if !decl.c_variadic || matches!(abi, ExternAbi::C { .. } | ExternAbi::Cdecl { .. }) {
125+
return;
118126
}
127+
128+
let extended_abi_support = tcx.features().extended_varargs_abi_support();
129+
let conventions = match (extended_abi_support, abi.supports_varargs()) {
130+
// User enabled additional ABI support for varargs and function ABI matches those ones.
131+
(true, true) => return,
132+
133+
// Using this ABI would be ok, if the feature for additional ABI support was enabled.
134+
// Return CONVENTIONS_STABLE, because we want the other error to look the same.
135+
(false, true) => {
136+
feature_err(&tcx.sess, sym::extended_varargs_abi_support, span, UNSTABLE_EXPLAIN)
137+
.emit();
138+
CONVENTIONS_STABLE
139+
}
140+
141+
(false, false) => CONVENTIONS_STABLE,
142+
(true, false) => CONVENTIONS_UNSTABLE,
143+
};
144+
145+
tcx.dcx().emit_err(errors::VariadicFunctionCompatibleConvention { span, conventions });
119146
}
120147

121148
pub fn provide(providers: &mut Providers) {

compiler/rustc_hir_typeck/src/pat.rs

+56-24
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
699699

700700
// Determine the binding mode...
701701
let bm = match user_bind_annot {
702-
BindingMode(ByRef::No, Mutability::Mut) if matches!(def_br, ByRef::Yes(_)) => {
702+
BindingMode(ByRef::No, Mutability::Mut) if let ByRef::Yes(def_br_mutbl) = def_br => {
703703
if pat.span.at_least_rust_2024()
704704
&& (self.tcx.features().ref_pat_eat_one_layer_2024()
705705
|| self.tcx.features().ref_pat_eat_one_layer_2024_structural())
@@ -719,22 +719,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
719719
// `mut` resets the binding mode on edition <= 2021
720720
self.add_rust_2024_migration_desugared_pat(
721721
pat_info.top_info.hir_id,
722-
pat.span,
722+
pat,
723723
ident.span,
724-
"requires binding by-value, but the implicit default is by-reference",
724+
def_br_mutbl,
725725
);
726726
BindingMode(ByRef::No, Mutability::Mut)
727727
}
728728
}
729729
BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
730730
BindingMode(ByRef::Yes(_), _) => {
731-
if matches!(def_br, ByRef::Yes(_)) {
731+
if let ByRef::Yes(def_br_mutbl) = def_br {
732732
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
733733
self.add_rust_2024_migration_desugared_pat(
734734
pat_info.top_info.hir_id,
735-
pat.span,
735+
pat,
736736
ident.span,
737-
"cannot override to bind by-reference when that is the implicit default",
737+
def_br_mutbl,
738738
);
739739
}
740740
user_bind_annot
@@ -2263,13 +2263,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22632263
}
22642264
} else {
22652265
// Reset binding mode on old editions
2266-
if pat_info.binding_mode != ByRef::No {
2266+
if let ByRef::Yes(inh_mut) = pat_info.binding_mode {
22672267
pat_info.binding_mode = ByRef::No;
22682268
self.add_rust_2024_migration_desugared_pat(
22692269
pat_info.top_info.hir_id,
2270-
pat.span,
2270+
pat,
22712271
inner.span,
2272-
"cannot implicitly match against multiple layers of reference",
2272+
inh_mut,
22732273
)
22742274
}
22752275
}
@@ -2635,33 +2635,65 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26352635
fn add_rust_2024_migration_desugared_pat(
26362636
&self,
26372637
pat_id: HirId,
2638-
subpat_span: Span,
2638+
subpat: &'tcx Pat<'tcx>,
26392639
cutoff_span: Span,
2640-
detailed_label: &str,
2640+
def_br_mutbl: Mutability,
26412641
) {
26422642
// Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
26432643
// If the subpattern's span is is from an expansion, the emitted label will not be trimmed.
26442644
let source_map = self.tcx.sess.source_map();
26452645
let cutoff_span = source_map
2646-
.span_extend_prev_while(cutoff_span, char::is_whitespace)
2646+
.span_extend_prev_while(cutoff_span, |c| c.is_whitespace() || c == '(')
26472647
.unwrap_or(cutoff_span);
2648-
// Ensure we use the syntax context and thus edition of `subpat_span`; this will be a hard
2648+
// Ensure we use the syntax context and thus edition of `subpat.span`; this will be a hard
26492649
// error if the subpattern is of edition >= 2024.
2650-
let trimmed_span = subpat_span.until(cutoff_span).with_ctxt(subpat_span.ctxt());
2650+
let trimmed_span = subpat.span.until(cutoff_span).with_ctxt(subpat.span.ctxt());
2651+
2652+
let mut typeck_results = self.typeck_results.borrow_mut();
2653+
let mut table = typeck_results.rust_2024_migration_desugared_pats_mut();
2654+
// FIXME(ref_pat_eat_one_layer_2024): The migration diagnostic doesn't know how to track the
2655+
// default binding mode in the presence of Rule 3 or Rule 5. As a consequence, the labels it
2656+
// gives for default binding modes are wrong, as well as suggestions based on the default
2657+
// binding mode. This keeps it from making those suggestions, as doing so could panic.
2658+
let info = table.entry(pat_id).or_insert_with(|| ty::Rust2024IncompatiblePatInfo {
2659+
primary_labels: Vec::new(),
2660+
bad_modifiers: false,
2661+
bad_ref_pats: false,
2662+
suggest_eliding_modes: !self.tcx.features().ref_pat_eat_one_layer_2024()
2663+
&& !self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
2664+
});
26512665

26522666
// Only provide a detailed label if the problematic subpattern isn't from an expansion.
26532667
// In the case that it's from a macro, we'll add a more detailed note in the emitter.
2654-
let desc = if subpat_span.from_expansion() {
2655-
"default binding mode is reset within expansion"
2668+
let from_expansion = subpat.span.from_expansion();
2669+
let primary_label = if from_expansion {
2670+
// NB: This wording assumes the only expansions that can produce problematic reference
2671+
// patterns and bindings are macros. If a desugaring or AST pass is added that can do
2672+
// so, we may want to inspect the span's source callee or macro backtrace.
2673+
"occurs within macro expansion".to_owned()
26562674
} else {
2657-
detailed_label
2675+
let pat_kind = if let PatKind::Binding(user_bind_annot, _, _, _) = subpat.kind {
2676+
info.bad_modifiers |= true;
2677+
// If the user-provided binding modifier doesn't match the default binding mode, we'll
2678+
// need to suggest reference patterns, which can affect other bindings.
2679+
// For simplicity, we opt to suggest making the pattern fully explicit.
2680+
info.suggest_eliding_modes &=
2681+
user_bind_annot == BindingMode(ByRef::Yes(def_br_mutbl), Mutability::Not);
2682+
"binding modifier"
2683+
} else {
2684+
info.bad_ref_pats |= true;
2685+
// For simplicity, we don't try to suggest eliding reference patterns. Thus, we'll
2686+
// suggest adding them instead, which can affect the types assigned to bindings.
2687+
// As such, we opt to suggest making the pattern fully explicit.
2688+
info.suggest_eliding_modes = false;
2689+
"reference pattern"
2690+
};
2691+
let dbm_str = match def_br_mutbl {
2692+
Mutability::Not => "ref",
2693+
Mutability::Mut => "ref mut",
2694+
};
2695+
format!("{pat_kind} not allowed under `{dbm_str}` default binding mode")
26582696
};
2659-
2660-
self.typeck_results
2661-
.borrow_mut()
2662-
.rust_2024_migration_desugared_pats_mut()
2663-
.entry(pat_id)
2664-
.or_default()
2665-
.push((trimmed_span, desc.to_owned()));
2697+
info.primary_labels.push((trimmed_span, primary_label));
26662698
}
26672699
}

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub use self::sty::{
9595
pub use self::trait_def::TraitDef;
9696
pub use self::typeck_results::{
9797
CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, IsIdentity,
98-
TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
98+
Rust2024IncompatiblePatInfo, TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
9999
};
100100
pub use self::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
101101
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};

0 commit comments

Comments
 (0)