Skip to content

Commit 9aac78a

Browse files
committed
Auto merge of #119918 - matthiaskrgr:rollup-kjjh51l, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #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 284cb71 + 7144248 commit 9aac78a

25 files changed

+255
-91
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_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_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_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
}

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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: the trait bound `T: Trait` is not satisfied
2+
--> $DIR/assoc-type-const-bound-usage-0.rs:21:6
3+
|
4+
LL | <T as /* FIXME: ~const */ Trait>::Assoc::func()
5+
| ^ the trait `Trait` is not implemented for `T`
6+
|
7+
help: consider further restricting this bound
8+
|
9+
LL | const fn qualified<T: ~const Trait + Trait>() -> i32 {
10+
| +++++++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// FIXME(effects): Collapse the revisions into one once we support `<Ty as ~const Trait>::Proj`.
2+
// revisions: unqualified qualified
3+
//[unqualified] check-pass
4+
//[qualified] known-bug: unknown
5+
6+
#![feature(const_trait_impl, effects)]
7+
8+
#[const_trait]
9+
trait Trait {
10+
type Assoc: ~const Trait;
11+
fn func() -> i32;
12+
}
13+
14+
#[cfg(unqualified)]
15+
const fn unqualified<T: ~const Trait>() -> i32 {
16+
T::Assoc::func()
17+
}
18+
19+
#[cfg(qualified)]
20+
const fn qualified<T: ~const Trait>() -> i32 {
21+
<T as /* FIXME: ~const */ Trait>::Assoc::func()
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: the trait bound `T: Trait` is not satisfied
2+
--> $DIR/assoc-type-const-bound-usage-1.rs:23:43
3+
|
4+
LL | fn qualified<T: const Trait>() -> Type<{ <T as /* FIXME: const */ Trait>::Assoc::func() }> {
5+
| ^ the trait `Trait` is not implemented for `T`
6+
|
7+
help: consider further restricting this bound
8+
|
9+
LL | fn qualified<T: const Trait + Trait>() -> Type<{ <T as /* FIXME: const */ Trait>::Assoc::func() }> {
10+
| +++++++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// FIXME(effects): Collapse the revisions into one once we support `<Ty as const Trait>::Proj`.
2+
// revisions: unqualified qualified
3+
//[unqualified] check-pass
4+
//[qualified] known-bug: unknown
5+
6+
#![feature(const_trait_impl, effects, generic_const_exprs)]
7+
#![allow(incomplete_features)]
8+
9+
#[const_trait]
10+
trait Trait {
11+
type Assoc: ~const Trait;
12+
fn func() -> i32;
13+
}
14+
15+
struct Type<const N: i32>;
16+
17+
#[cfg(unqualified)]
18+
fn unqualified<T: const Trait>() -> Type<{ T::Assoc::func() }> {
19+
Type
20+
}
21+
22+
#[cfg(qualified)]
23+
fn qualified<T: const Trait>() -> Type<{ <T as /* FIXME: const */ Trait>::Assoc::func() }> {
24+
Type
25+
}
26+
27+
fn main() {}

tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage.rs

-15
This file was deleted.

0 commit comments

Comments
 (0)