Skip to content

Commit c6c4abf

Browse files
committed
Auto merge of #119927 - matthiaskrgr:rollup-885ws57, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #119587 (Varargs support for system ABI) - #119891 (rename `reported_signature_mismatch` to reflect its use) - #119894 (Allow `~const` on associated type bounds again) - #119896 (Taint `_` placeholder types in trait impl method signatures) - #119898 (Remove unused `ErrorReporting` variant from overflow handling) - #119902 (fix typo in `fn()` docs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1d8d7b1 + f53caa1 commit c6c4abf

33 files changed

+289
-104
lines changed

compiler/rustc_ast_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ ast_passes_tilde_const_disallowed = `~const` is not allowed here
232232
.trait = this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
233233
.trait_impl = this impl is not `const`, so it cannot have `~const` trait bounds
234234
.impl = inherent impls cannot have `~const` trait bounds
235+
.trait_assoc_ty = associated types in non-`#[const_trait]` traits cannot have `~const` trait bounds
236+
.trait_impl_assoc_ty = associated types in non-const impls cannot have `~const` trait bounds
237+
.inherent_assoc_ty = inherent associated types cannot have `~const` trait bounds
235238
.object = trait objects cannot have `~const` trait bounds
236239
.item = this item cannot have `~const` trait bounds
237240

compiler/rustc_ast_passes/src/ast_validation.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ enum SelfSemantic {
3737
}
3838

3939
/// What is the context that prevents using `~const`?
40+
// FIXME(effects): Consider getting rid of this in favor of `errors::TildeConstReason`, they're
41+
// almost identical. This gets rid of an abstraction layer which might be considered bad.
4042
enum DisallowTildeConstContext<'a> {
4143
TraitObject,
4244
Fn(FnKind<'a>),
4345
Trait(Span),
4446
TraitImpl(Span),
4547
Impl(Span),
48+
TraitAssocTy(Span),
49+
TraitImplAssocTy(Span),
50+
InherentAssocTy(Span),
4651
Item,
4752
}
4853

@@ -316,6 +321,7 @@ impl<'a> AstValidator<'a> {
316321
constness: Const::No,
317322
polarity: ImplPolarity::Positive,
318323
trait_ref,
324+
..
319325
} = parent
320326
{
321327
Some(trait_ref.path.span.shrink_to_lo())
@@ -1286,6 +1292,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12861292
// suggestion for moving such bounds to the assoc const fns if available.
12871293
errors::TildeConstReason::Impl { span }
12881294
}
1295+
&DisallowTildeConstContext::TraitAssocTy(span) => {
1296+
errors::TildeConstReason::TraitAssocTy { span }
1297+
}
1298+
&DisallowTildeConstContext::TraitImplAssocTy(span) => {
1299+
errors::TildeConstReason::TraitImplAssocTy { span }
1300+
}
1301+
&DisallowTildeConstContext::InherentAssocTy(span) => {
1302+
errors::TildeConstReason::InherentAssocTy { span }
1303+
}
12891304
DisallowTildeConstContext::TraitObject => {
12901305
errors::TildeConstReason::TraitObject
12911306
}
@@ -1483,13 +1498,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14831498
self.check_item_named(item.ident, "const");
14841499
}
14851500

1501+
let parent_is_const =
1502+
self.outer_trait_or_trait_impl.as_ref().and_then(TraitOrTraitImpl::constness).is_some();
1503+
14861504
match &item.kind {
14871505
AssocItemKind::Fn(box Fn { sig, generics, body, .. })
1488-
if self
1489-
.outer_trait_or_trait_impl
1490-
.as_ref()
1491-
.and_then(TraitOrTraitImpl::constness)
1492-
.is_some()
1506+
if parent_is_const
14931507
|| ctxt == AssocCtxt::Trait
14941508
|| matches!(sig.header.constness, Const::Yes(_)) =>
14951509
{
@@ -1505,6 +1519,20 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15051519
);
15061520
self.visit_fn(kind, item.span, item.id);
15071521
}
1522+
AssocItemKind::Type(_) => {
1523+
let disallowed = (!parent_is_const).then(|| match self.outer_trait_or_trait_impl {
1524+
Some(TraitOrTraitImpl::Trait { .. }) => {
1525+
DisallowTildeConstContext::TraitAssocTy(item.span)
1526+
}
1527+
Some(TraitOrTraitImpl::TraitImpl { .. }) => {
1528+
DisallowTildeConstContext::TraitImplAssocTy(item.span)
1529+
}
1530+
None => DisallowTildeConstContext::InherentAssocTy(item.span),
1531+
});
1532+
self.with_tilde_const(disallowed, |this| {
1533+
this.with_in_trait_impl(None, |this| visit::walk_assoc_item(this, item, ctxt))
1534+
})
1535+
}
15081536
_ => self.with_in_trait_impl(None, |this| visit::walk_assoc_item(this, item, ctxt)),
15091537
}
15101538
}

compiler/rustc_ast_passes/src/errors.rs

+17
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ pub struct ConstBoundTraitObject {
565565
pub span: Span,
566566
}
567567

568+
// FIXME(effects): Consider making the note/reason the message of the diagnostic.
569+
// FIXME(effects): Provide structured suggestions (e.g., add `const` / `#[const_trait]` here).
568570
#[derive(Diagnostic)]
569571
#[diag(ast_passes_tilde_const_disallowed)]
570572
pub struct TildeConstDisallowed {
@@ -598,6 +600,21 @@ pub enum TildeConstReason {
598600
#[primary_span]
599601
span: Span,
600602
},
603+
#[note(ast_passes_trait_assoc_ty)]
604+
TraitAssocTy {
605+
#[primary_span]
606+
span: Span,
607+
},
608+
#[note(ast_passes_trait_impl_assoc_ty)]
609+
TraitImplAssocTy {
610+
#[primary_span]
611+
span: Span,
612+
},
613+
#[note(ast_passes_inherent_assoc_ty)]
614+
InherentAssocTy {
615+
#[primary_span]
616+
span: Span,
617+
},
601618
#[note(ast_passes_object)]
602619
TraitObject,
603620
#[note(ast_passes_item)]

compiler/rustc_hir_analysis/src/astconv/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26592659
self.suggest_trait_fn_ty_for_impl_fn_infer(hir_id, Some(i))
26602660
{
26612661
infer_replacements.push((a.span, suggested_ty.to_string()));
2662-
return suggested_ty;
2662+
return Ty::new_error_with_message(
2663+
self.tcx(),
2664+
a.span,
2665+
suggested_ty.to_string(),
2666+
);
26632667
}
26642668
}
26652669

@@ -2677,7 +2681,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26772681
self.suggest_trait_fn_ty_for_impl_fn_infer(hir_id, None)
26782682
{
26792683
infer_replacements.push((output.span, suggested_ty.to_string()));
2680-
suggested_ty
2684+
Ty::new_error_with_message(self.tcx(), output.span, suggested_ty.to_string())
26812685
} else {
26822686
visitor.visit_ty(output);
26832687
self.ast_ty_to_ty(output)

compiler/rustc_hir_analysis/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ use rustc_hir::def::DefKind;
116116
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
117117

118118
fn require_c_abi_if_c_variadic(tcx: TyCtxt<'_>, decl: &hir::FnDecl<'_>, abi: Abi, span: Span) {
119-
const CONVENTIONS_UNSTABLE: &str = "`C`, `cdecl`, `aapcs`, `win64`, `sysv64` or `efiapi`";
119+
const CONVENTIONS_UNSTABLE: &str =
120+
"`C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`";
120121
const CONVENTIONS_STABLE: &str = "`C` or `cdecl`";
121122
const UNSTABLE_EXPLAIN: &str =
122123
"using calling conventions other than `C` or `cdecl` for varargs functions is unstable";

compiler/rustc_infer/src/infer/at.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> InferCtxt<'tcx> {
8484
selection_cache: self.selection_cache.clone(),
8585
evaluation_cache: self.evaluation_cache.clone(),
8686
reported_trait_errors: self.reported_trait_errors.clone(),
87-
reported_closure_mismatch: self.reported_closure_mismatch.clone(),
87+
reported_signature_mismatch: self.reported_signature_mismatch.clone(),
8888
tainted_by_errors: self.tainted_by_errors.clone(),
8989
err_count_on_creation: self.err_count_on_creation,
9090
universe: self.universe.clone(),

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub struct InferCtxt<'tcx> {
278278
/// avoid reporting the same error twice.
279279
pub reported_trait_errors: RefCell<FxIndexMap<Span, Vec<ty::Predicate<'tcx>>>>,
280280

281-
pub reported_closure_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
281+
pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
282282

283283
/// When an error occurs, we want to avoid reporting "derived"
284284
/// errors that are due to this original failure. Normally, we
@@ -702,7 +702,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
702702
selection_cache: Default::default(),
703703
evaluation_cache: Default::default(),
704704
reported_trait_errors: Default::default(),
705-
reported_closure_mismatch: Default::default(),
705+
reported_signature_mismatch: Default::default(),
706706
tainted_by_errors: Cell::new(None),
707707
err_count_on_creation: tcx.dcx().err_count(),
708708
universe: Cell::new(ty::UniverseIndex::ROOT),

compiler/rustc_metadata/src/native_libs.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,23 @@ impl<'tcx> Collector<'tcx> {
520520
) -> DllImport {
521521
let span = self.tcx.def_span(item);
522522

523+
// this logic is similar to `Target::adjust_abi` (in rustc_target/src/spec/mod.rs) but errors on unsupported inputs
523524
let calling_convention = if self.tcx.sess.target.arch == "x86" {
524525
match abi {
525526
Abi::C { .. } | Abi::Cdecl { .. } => DllCallingConvention::C,
526-
Abi::Stdcall { .. } | Abi::System { .. } => {
527-
DllCallingConvention::Stdcall(self.i686_arg_list_size(item))
527+
Abi::Stdcall { .. } => DllCallingConvention::Stdcall(self.i686_arg_list_size(item)),
528+
// On Windows, `extern "system"` behaves like msvc's `__stdcall`.
529+
// `__stdcall` only applies on x86 and on non-variadic functions:
530+
// https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-170
531+
Abi::System { .. } => {
532+
let c_variadic =
533+
self.tcx.type_of(item).instantiate_identity().fn_sig(self.tcx).c_variadic();
534+
535+
if c_variadic {
536+
DllCallingConvention::C
537+
} else {
538+
DllCallingConvention::Stdcall(self.i686_arg_list_size(item))
539+
}
528540
}
529541
Abi::Fastcall { .. } => {
530542
DllCallingConvention::Fastcall(self.i686_arg_list_size(item))

compiler/rustc_middle/src/traits/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,6 @@ pub enum SelectionError<'tcx> {
611611
NotConstEvaluatable(NotConstEvaluatable),
612612
/// Exceeded the recursion depth during type projection.
613613
Overflow(OverflowError),
614-
/// Signaling that an error has already been emitted, to avoid
615-
/// multiple errors being shown.
616-
ErrorReporting,
617614
/// Computing an opaque type's hidden type caused an error (e.g. a cycle error).
618615
/// We can thus not know whether the hidden type implements an auto trait, so
619616
/// we should not presume anything about it.

compiler/rustc_middle/src/traits/select.rs

-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ impl EvaluationResult {
302302
pub enum OverflowError {
303303
Error(ErrorGuaranteed),
304304
Canonical,
305-
ErrorReporting,
306305
}
307306

308307
impl From<ErrorGuaranteed> for OverflowError {
@@ -318,7 +317,6 @@ impl<'tcx> From<OverflowError> for SelectionError<'tcx> {
318317
match overflow_error {
319318
OverflowError::Error(e) => SelectionError::Overflow(OverflowError::Error(e)),
320319
OverflowError::Canonical => SelectionError::Overflow(OverflowError::Canonical),
321-
OverflowError::ErrorReporting => SelectionError::ErrorReporting,
322320
}
323321
}
324322
}

compiler/rustc_target/src/abi/call/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
840840
"sparc" => sparc::compute_abi_info(cx, self),
841841
"sparc64" => sparc64::compute_abi_info(cx, self),
842842
"nvptx64" => {
843-
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::PtxKernel {
843+
if cx.target_spec().adjust_abi(abi, self.c_variadic) == spec::abi::Abi::PtxKernel {
844844
nvptx64::compute_ptx_kernel_abi_info(cx, self)
845845
} else {
846846
nvptx64::compute_abi_info(self)
@@ -849,7 +849,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
849849
"hexagon" => hexagon::compute_abi_info(self),
850850
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
851851
"wasm32" | "wasm64" => {
852-
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::Wasm {
852+
if cx.target_spec().adjust_abi(abi, self.c_variadic) == spec::abi::Abi::Wasm {
853853
wasm::compute_wasm_abi_info(self)
854854
} else {
855855
wasm::compute_c_abi_info(cx, self)

compiler/rustc_target/src/spec/abi/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ impl Abi {
7070
// * C and Cdecl obviously support varargs.
7171
// * C can be based on Aapcs, SysV64 or Win64, so they must support varargs.
7272
// * EfiApi is based on Win64 or C, so it also supports it.
73+
// * System falls back to C for functions with varargs.
7374
//
7475
// * Stdcall does not, because it would be impossible for the callee to clean
7576
// up the arguments. (callee doesn't know how many arguments are there)
7677
// * Same for Fastcall, Vectorcall and Thiscall.
77-
// * System can become Stdcall, so is also a no-no.
7878
// * Other calling conventions are related to hardware or the compiler itself.
7979
match self {
8080
Self::C { .. }
8181
| Self::Cdecl { .. }
82+
| Self::System { .. }
8283
| Self::Aapcs { .. }
8384
| Self::Win64 { .. }
8485
| Self::SysV64 { .. }

compiler/rustc_target/src/spec/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2401,10 +2401,14 @@ impl DerefMut for Target {
24012401

24022402
impl Target {
24032403
/// Given a function ABI, turn it into the correct ABI for this target.
2404-
pub fn adjust_abi(&self, abi: Abi) -> Abi {
2404+
pub fn adjust_abi(&self, abi: Abi, c_variadic: bool) -> Abi {
24052405
match abi {
24062406
Abi::C { .. } => self.default_adjusted_cabi.unwrap_or(abi),
2407-
Abi::System { unwind } if self.is_like_windows && self.arch == "x86" => {
2407+
2408+
// On Windows, `extern "system"` behaves like msvc's `__stdcall`.
2409+
// `__stdcall` only applies on x86 and on non-variadic functions:
2410+
// https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-170
2411+
Abi::System { unwind } if self.is_like_windows && self.arch == "x86" && !c_variadic => {
24082412
Abi::Stdcall { unwind }
24092413
}
24102414
Abi::System { unwind } => Abi::C { unwind },

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -947,9 +947,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
947947
Overflow(_) => {
948948
bug!("overflow should be handled before the `report_selection_error` path");
949949
}
950-
SelectionError::ErrorReporting => {
951-
bug!("ErrorReporting Overflow should not reach `report_selection_err` call")
952-
}
953950
};
954951

955952
self.note_obligation_cause(&mut err, &obligation);
@@ -3459,14 +3456,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
34593456
let found_node = found_did.and_then(|did| self.tcx.hir().get_if_local(did));
34603457
let found_span = found_did.and_then(|did| self.tcx.hir().span_if_local(did));
34613458

3462-
if self.reported_closure_mismatch.borrow().contains(&(span, found_span)) {
3459+
if !self.reported_signature_mismatch.borrow_mut().insert((span, found_span)) {
34633460
// We check closures twice, with obligations flowing in different directions,
34643461
// but we want to complain about them only once.
34653462
return None;
34663463
}
34673464

3468-
self.reported_closure_mismatch.borrow_mut().insert((span, found_span));
3469-
34703465
let mut not_tupled = false;
34713466

34723467
let found = match found_trait_ref.skip_binder().args.type_at(1).kind() {

compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs

-2
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
116116
r,
117117
)
118118
}
119-
OverflowError::ErrorReporting => EvaluationResult::EvaluatedToErr,
120119
OverflowError::Error(_) => EvaluationResult::EvaluatedToErr,
121120
})
122121
}
123-
Err(OverflowError::ErrorReporting) => EvaluationResult::EvaluatedToErr,
124122
Err(OverflowError::Error(_)) => EvaluationResult::EvaluatedToErr,
125123
}
126124
}

compiler/rustc_trait_selection/src/traits/select/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use super::util;
1414
use super::util::closure_trait_ref_and_return_type;
1515
use super::wf;
1616
use super::{
17-
ErrorReporting, ImplDerivedObligation, ImplDerivedObligationCause, Normalized, Obligation,
18-
ObligationCause, ObligationCauseCode, Overflow, PolyTraitObligation, PredicateObligation,
19-
Selection, SelectionError, SelectionResult, TraitQueryMode,
17+
ImplDerivedObligation, ImplDerivedObligationCause, Normalized, Obligation, ObligationCause,
18+
ObligationCauseCode, Overflow, PolyTraitObligation, PredicateObligation, Selection,
19+
SelectionError, SelectionResult, TraitQueryMode,
2020
};
2121

2222
use crate::infer::{InferCtxt, InferOk, TypeFreshener};
@@ -496,7 +496,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
496496
}
497497
Ok(_) => Ok(None),
498498
Err(OverflowError::Canonical) => Err(Overflow(OverflowError::Canonical)),
499-
Err(OverflowError::ErrorReporting) => Err(ErrorReporting),
500499
Err(OverflowError::Error(e)) => Err(Overflow(OverflowError::Error(e))),
501500
})
502501
.flat_map(Result::transpose)
@@ -1233,7 +1232,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12331232
Ok(Some(c)) => self.evaluate_candidate(stack, &c),
12341233
Ok(None) => Ok(EvaluatedToAmbig),
12351234
Err(Overflow(OverflowError::Canonical)) => Err(OverflowError::Canonical),
1236-
Err(ErrorReporting) => Err(OverflowError::ErrorReporting),
12371235
Err(..) => Ok(EvaluatedToErr),
12381236
}
12391237
}

compiler/rustc_ty_utils/src/abi.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ fn fn_sig_for_fn_abi<'tcx>(
228228
}
229229

230230
#[inline]
231-
fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi) -> Conv {
231+
fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi, c_variadic: bool) -> Conv {
232232
use rustc_target::spec::abi::Abi::*;
233-
match tcx.sess.target.adjust_abi(abi) {
233+
match tcx.sess.target.adjust_abi(abi, c_variadic) {
234234
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => Conv::Rust,
235235

236236
// This is intentionally not using `Conv::Cold`, as that has to preserve
@@ -488,7 +488,7 @@ fn fn_abi_new_uncached<'tcx>(
488488
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> {
489489
let sig = cx.tcx.normalize_erasing_late_bound_regions(cx.param_env, sig);
490490

491-
let conv = conv_from_spec_abi(cx.tcx(), sig.abi);
491+
let conv = conv_from_spec_abi(cx.tcx(), sig.abi, sig.c_variadic);
492492

493493
let mut inputs = sig.inputs();
494494
let extra_args = if sig.abi == RustCall {

library/core/src/primitive_docs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1605,9 +1605,9 @@ mod prim_ref {}
16051605
/// type in the function pointer to the type at the function declaration, and the return value is
16061606
/// [`transmute`d][mem::transmute] from the type in the declaration to the type in the
16071607
/// pointer. All the usual caveats and concerns around transmutation apply; for instance, if the
1608-
/// function expects a `NonNullI32` and the function pointer uses the ABI-compatible type
1609-
/// `Option<NonNullI32>`, and the value used for the argument is `None`, then this call is Undefined
1610-
/// Behavior since transmuting `None::<NonNullI32>` to `NonNullI32` violates the non-null
1608+
/// function expects a `NonZeroI32` and the function pointer uses the ABI-compatible type
1609+
/// `Option<NonZeroI32>`, and the value used for the argument is `None`, then this call is Undefined
1610+
/// Behavior since transmuting `None::<NonZeroI32>` to `NonZeroI32` violates the non-zero
16111611
/// requirement.
16121612
///
16131613
/// #### Requirements concerning target features

src/librustdoc/clean/blanket_impl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
8181
match infcx.evaluate_obligation(&obligation) {
8282
Ok(eval_result) if eval_result.may_apply() => {}
8383
Err(traits::OverflowError::Canonical) => {}
84-
Err(traits::OverflowError::ErrorReporting) => {}
8584
_ => continue 'blanket_impls,
8685
}
8786
}

0 commit comments

Comments
 (0)