Skip to content

Commit e83b476

Browse files
authoredNov 11, 2022
Rollup merge of #104216 - Nilstrieb:dont-ice-invalid-operator-traits, r=estebank
Don't ICE on operator trait methods with generic methods Emit a fatal error instead. fixes #104213
2 parents f3931c8 + cedaaa6 commit e83b476

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed
 

‎compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,6 @@ hir_analysis_const_bound_for_non_const_trait =
150150
hir_analysis_self_in_impl_self =
151151
`Self` is not valid in the self type of an impl block
152152
.note = replace `Self` with a different type
153+
154+
hir_analysis_op_trait_generic_params =
155+
`{$method_name}` must not have any generic parameters

‎compiler/rustc_hir_typeck/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,11 @@ pub struct AddMissingParenthesesInRange {
125125
#[suggestion_part(code = ")")]
126126
pub right: Span,
127127
}
128+
129+
#[derive(Diagnostic)]
130+
#[diag(hir_analysis_op_trait_generic_params)]
131+
pub struct OpMethodGenericParams {
132+
#[primary_span]
133+
pub span: Span,
134+
pub method_name: String,
135+
}

‎compiler/rustc_hir_typeck/src/method/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod suggest;
1010
pub use self::suggest::SelfSource;
1111
pub use self::MethodError::*;
1212

13+
use crate::errors::OpMethodGenericParams;
1314
use crate::{Expectation, FnCtxt};
1415
use rustc_data_structures::sync::Lrc;
1516
use rustc_errors::{Applicability, Diagnostic};
@@ -443,7 +444,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
443444
};
444445
let def_id = method_item.def_id;
445446
let generics = tcx.generics_of(def_id);
446-
assert_eq!(generics.params.len(), 0);
447+
448+
if generics.params.len() != 0 {
449+
tcx.sess.emit_fatal(OpMethodGenericParams {
450+
span: tcx.def_span(method_item.def_id),
451+
method_name: m_name.to_string(),
452+
});
453+
}
447454

448455
debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);
449456
let mut obligations = vec![];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![crate_type = "lib"]
2+
#![feature(lang_items)]
3+
#![feature(no_core)]
4+
#![no_core]
5+
6+
#[lang="sized"]
7+
pub trait Sized {
8+
// Empty.
9+
}
10+
11+
#[lang = "add"]
12+
trait Add<RHS=Self> {
13+
type Output;
14+
15+
fn add<Y>(self, _: RHS) -> Self::Output;
16+
//~^ ERROR `add` must not have any generic parameters
17+
}
18+
19+
#[allow(unreachable_code)]
20+
fn ice(a: usize) {
21+
let r = loop {};
22+
r = r + a;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `add` must not have any generic parameters
2+
--> $DIR/invalid_operator_trait.rs:15:5
3+
|
4+
LL | fn add<Y>(self, _: RHS) -> Self::Output;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)
Please sign in to comment.