Skip to content

Commit 7985e4c

Browse files
authored
Rollup merge of #87524 - FabianWolff:issue-87495, r=Aaron1011
Fix ICE in `diagnostic_hir_wf_check` Fixes #87495. The [documentation for `ObligationCauseCode::WellFormed`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/traits/enum.ObligationCauseCode.html#variant.WellFormed) says that > it is always correct [...] to specify `WellFormed(None)` instead of `WellFormed(Some(...))`, which seems to have caused the issue here, as `diagnostic_hir_wf_check` does not expect to be called with an associated constant and will ICE: https://github.com/rust-lang/rust/blob/fd853c00e255559255885aadff9e93a1760c8728/compiler/rustc_typeck/src/hir_wf_check.rs#L131-L134 Therefore, I have changed `check_associated_item()` to pass a `WellFormed(None)` for associated constants. r? ``@Aaron1011``
2 parents 4ae5296 + dbd0fd2 commit 7985e4c

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
240240

241241
let mut err = match *error {
242242
SelectionError::Unimplemented => {
243-
// If this obligation was generated as a result of well-formed checking, see if we
244-
// can get a better error message by performing HIR-based well formed checking.
243+
// If this obligation was generated as a result of well-formedness checking, see if we
244+
// can get a better error message by performing HIR-based well-formedness checking.
245245
if let ObligationCauseCode::WellFormed(Some(wf_loc)) =
246246
root_obligation.cause.code.peel_derives()
247247
{

compiler/rustc_typeck/src/hir_wf_check.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ fn diagnostic_hir_wf_check<'tcx>(
3838
// given the type `Option<MyStruct<u8>>`, we will check
3939
// `Option<MyStruct<u8>>`, `MyStruct<u8>`, and `u8`.
4040
// For each type, we perform a well-formed check, and see if we get
41-
// an erorr that matches our expected predicate. We keep save
41+
// an error that matches our expected predicate. We save
4242
// the `ObligationCause` corresponding to the *innermost* type,
4343
// which is the most specific type that we can point to.
4444
// In general, the different components of an `hir::Ty` may have
45-
// completely differentr spans due to macro invocations. Pointing
45+
// completely different spans due to macro invocations. Pointing
4646
// to the most accurate part of the type can be the difference
4747
// between a useless span (e.g. the macro invocation site)
48-
// and a useful span (e.g. a user-provided type passed in to the macro).
48+
// and a useful span (e.g. a user-provided type passed into the macro).
4949
//
5050
// This approach is quite inefficient - we redo a lot of work done
5151
// by the normal WF checker. However, this code is run at most once
5252
// per reported error - it will have no impact when compilation succeeds,
53-
// and should only have an impact if a very large number of errors are
54-
// displaydd to the user.
53+
// and should only have an impact if a very large number of errors is
54+
// displayed to the user.
5555
struct HirWfCheck<'tcx> {
5656
tcx: TyCtxt<'tcx>,
5757
predicate: ty::Predicate<'tcx>,
@@ -126,10 +126,12 @@ fn diagnostic_hir_wf_check<'tcx>(
126126
WellFormedLoc::Ty(_) => match hir.get(hir_id) {
127127
hir::Node::ImplItem(item) => match item.kind {
128128
hir::ImplItemKind::TyAlias(ty) => Some(ty),
129+
hir::ImplItemKind::Const(ty, _) => Some(ty),
129130
ref item => bug!("Unexpected ImplItem {:?}", item),
130131
},
131132
hir::Node::TraitItem(item) => match item.kind {
132133
hir::TraitItemKind::Type(_, ty) => ty,
134+
hir::TraitItemKind::Const(ty, _) => Some(ty),
133135
ref item => bug!("Unexpected TraitItem {:?}", item),
134136
},
135137
hir::Node::Item(item) => match item.kind {

src/test/ui/wf/issue-87495.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for the ICE described in #87495.
2+
3+
trait T {
4+
const CONST: (bool, dyn T);
5+
//~^ ERROR: the trait `T` cannot be made into an object [E0038]
6+
}
7+
8+
fn main() {}

src/test/ui/wf/issue-87495.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0038]: the trait `T` cannot be made into an object
2+
--> $DIR/issue-87495.rs:4:25
3+
|
4+
LL | const CONST: (bool, dyn T);
5+
| ^^^^^ `T` cannot be made into an object
6+
|
7+
= help: consider moving `CONST` to another trait
8+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
9+
--> $DIR/issue-87495.rs:4:11
10+
|
11+
LL | trait T {
12+
| - this trait cannot be made into an object...
13+
LL | const CONST: (bool, dyn T);
14+
| ^^^^^ ...because it contains this associated `const`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)