Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't ICE on operator trait methods with generic methods #104216

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ hir_analysis_const_bound_for_non_const_trait =
hir_analysis_self_in_impl_self =
`Self` is not valid in the self type of an impl block
.note = replace `Self` with a different type

hir_analysis_op_trait_generic_params =
`{$method_name}` must not have any generic parameters
8 changes: 8 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ pub struct AddMissingParenthesesInRange {
#[suggestion_part(code = ")")]
pub right: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_op_trait_generic_params)]
pub struct OpMethodGenericParams {
#[primary_span]
pub span: Span,
pub method_name: String,
}
9 changes: 8 additions & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod suggest;
pub use self::suggest::SelfSource;
pub use self::MethodError::*;

use crate::errors::OpMethodGenericParams;
use crate::{Expectation, FnCtxt};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{Applicability, Diagnostic};
Expand Down Expand Up @@ -443,7 +444,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let def_id = method_item.def_id;
let generics = tcx.generics_of(def_id);
assert_eq!(generics.params.len(), 0);

if generics.params.len() != 0 {
tcx.sess.emit_fatal(OpMethodGenericParams {
Copy link
Member

@compiler-errors compiler-errors Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be fatal? Fatal errors should be reserved for particularly unrecoverable cases, and this one doesn't seem (at least initially) like one to me.

We could just return None here -- what does that do to the error reporting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a basic emit_err it causes further ICEs in other places. I haven't tried returning None but I assume it's gonna emit further errors about things not being found.

This code path is only hit when using no_core in weird ways (like by being a fuzzer) so I don't think we should care too much about recovery.

span: tcx.def_span(method_item.def_id),
method_name: m_name.to_string(),
});
}

debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);
let mut obligations = vec![];
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/traits/invalid_operator_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![crate_type = "lib"]
#![feature(lang_items)]
#![feature(no_core)]
#![no_core]

#[lang="sized"]
pub trait Sized {
// Empty.
}

#[lang = "add"]
trait Add<RHS=Self> {
type Output;

fn add<Y>(self, _: RHS) -> Self::Output;
//~^ ERROR `add` must not have any generic parameters
}

#[allow(unreachable_code)]
fn ice(a: usize) {
let r = loop {};
r = r + a;
}
8 changes: 8 additions & 0 deletions src/test/ui/traits/invalid_operator_trait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: `add` must not have any generic parameters
--> $DIR/invalid_operator_trait.rs:15:5
|
LL | fn add<Y>(self, _: RHS) -> Self::Output;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error