-
Notifications
You must be signed in to change notification settings - Fork 13.3k
correct spans in parem_env to deduplicate errors #102502
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use rustc_data_structures::fx::FxIndexSet; | ||
use rustc_hir as hir; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_infer::traits::util::elaborate_obligations; | ||
use rustc_infer::traits::Obligation; | ||
use rustc_middle::ty::{self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt}; | ||
use rustc_trait_selection::traits; | ||
|
||
|
@@ -110,7 +112,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { | |
} | ||
// Compute the bounds on Self and the type parameters. | ||
|
||
let ty::InstantiatedPredicates { mut predicates, .. } = | ||
let ty::InstantiatedPredicates { mut predicates, spans } = | ||
tcx.predicates_of(def_id).instantiate_identity(tcx); | ||
|
||
// Finally, we have to normalize the bounds in the environment, in | ||
|
@@ -132,6 +134,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { | |
|
||
let local_did = def_id.as_local(); | ||
let hir_id = local_did.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)); | ||
let mut is_fn = false; | ||
|
||
let constness = match hir_id { | ||
Some(hir_id) => match tcx.hir().get(hir_id) { | ||
|
@@ -181,8 +184,11 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { | |
kind: | ||
hir::ItemKind::Fn(hir::FnSig { header: hir::FnHeader { constness, .. }, .. }, ..), | ||
.. | ||
}) | ||
| hir::Node::TraitItem(hir::TraitItem { | ||
}) => { | ||
is_fn = true; | ||
*constness | ||
} | ||
hir::Node::TraitItem(hir::TraitItem { | ||
kind: | ||
hir::TraitItemKind::Fn( | ||
hir::FnSig { header: hir::FnHeader { constness, .. }, .. }, | ||
|
@@ -214,8 +220,36 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { | |
_ => hir::CRATE_HIR_ID, | ||
}; | ||
|
||
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id); | ||
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause) | ||
let span = tcx.def_span(def_id); | ||
|
||
if is_fn { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm skeptical that all this extra code is worthwhile... since all it's here for is to deduplicate a diagnostic that's still going to end up being an error. Especially because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing it out. If that isn't worthwhile, could you take a look at the first commit, which is just a little optimization of param_env, and I hope it's acceptable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this is an optimization in reality? Maybe you should put it up as a separate commit (separate from the span modifications) so we can do a perf run on it. I am still not confident that the added complexity is worth it. |
||
let mut spans = spans.into_iter(); | ||
let obligations: Vec<_> = predicates | ||
.into_iter() | ||
.map(|predicate| { | ||
let cause = traits::ObligationCause::misc(spans.next().unwrap_or(span), body_id); | ||
Obligation::new(cause, ty::ParamEnv::empty(), predicate) | ||
}) | ||
.collect(); | ||
|
||
let (predicates, causes): (Vec<_>, Vec<_>) = elaborate_obligations(tcx, obligations) | ||
.map(|obligation| (obligation.predicate, obligation.cause)) | ||
.unzip(); | ||
|
||
traits::normalize_param_env_with_causes( | ||
tcx, | ||
unnormalized_env, | ||
span, | ||
causes.into_iter(), | ||
predicates, | ||
) | ||
} else { | ||
traits::normalize_param_env_or_error( | ||
tcx, | ||
unnormalized_env, | ||
traits::ObligationCause::misc(span, body_id), | ||
) | ||
} | ||
} | ||
|
||
/// Elaborate the environment. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// compile-flags: -Zdeduplicate-diagnostics=yes | ||
|
||
fn f<T>(_: impl A<X = T>) {} | ||
//~^ ERROR the trait bound `T: B` is not satisfied [E0277] | ||
trait A: C<Z = <<Self as A>::X as B>::Y> { | ||
type X: B; | ||
} | ||
|
||
trait B { | ||
type Y; | ||
} | ||
|
||
trait C { | ||
type Z; | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0277]: the trait bound `T: B` is not satisfied | ||
--> $DIR/issue-102185.rs:3:17 | ||
| | ||
LL | fn f<T>(_: impl A<X = T>) {} | ||
| ^^^^^^^^ the trait `B` is not implemented for `T` | ||
| | ||
help: consider restricting type parameter `T` | ||
| | ||
LL | fn f<T: B>(_: impl A<X = T>) {} | ||
| +++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the use site, should we just take
&mut
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, as mentioned above, fully_normalize requires taking ownership of the predicats. I could not think of a good way to deal with this for a while.