Skip to content

Commit 9bc484f

Browse files
Reject more global bounds (HRTBs and those trivial after normalization)
1 parent 027a232 commit 9bc484f

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

compiler/rustc_typeck/src/check/wfcheck.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_infer::infer::outlives::obligations::TypeOutlives;
1717
use rustc_infer::infer::region_constraints::GenericKind;
1818
use rustc_infer::infer::{self, RegionckMode};
1919
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
20+
use rustc_infer::traits::TraitEngine;
2021
use rustc_middle::hir::nested_filter;
2122
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts, Subst};
2223
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -1796,7 +1797,7 @@ fn report_bivariance(
17961797

17971798
/// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that
17981799
/// aren't true.
1799-
fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirId) {
1800+
fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, span: Span, id: hir::HirId) {
18001801
let empty_env = ty::ParamEnv::empty();
18011802

18021803
let def_id = fcx.tcx.hir().local_def_id(id);
@@ -1807,33 +1808,64 @@ fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirI
18071808

18081809
for obligation in implied_obligations {
18091810
let pred = obligation.predicate;
1811+
1812+
// only use the span of the predicate clause (#90869)
1813+
let hir_node = fcx.tcx.hir().find(id);
1814+
let span = if let Some(hir::Generics { where_clause, .. }) =
1815+
hir_node.and_then(|node| node.generics())
1816+
{
1817+
let obligation_span = obligation.cause.span(fcx.tcx);
1818+
where_clause
1819+
.predicates
1820+
.iter()
1821+
// There seems to be no better way to find out which predicate we are in
1822+
.find(|pred| pred.span().contains(obligation_span))
1823+
.map(|pred| pred.span())
1824+
.unwrap_or(obligation_span)
1825+
} else {
1826+
span
1827+
};
1828+
18101829
// Match the existing behavior.
18111830
if pred.is_global() && !pred.has_late_bound_regions() {
18121831
let pred = fcx.normalize_associated_types_in(span, pred);
1813-
let hir_node = fcx.tcx.hir().find(id);
1814-
1815-
// only use the span of the predicate clause (#90869)
1816-
1817-
if let Some(hir::Generics { where_clause, .. }) =
1818-
hir_node.and_then(|node| node.generics())
1819-
{
1820-
let obligation_span = obligation.cause.span(fcx.tcx);
1821-
1822-
span = where_clause
1823-
.predicates
1824-
.iter()
1825-
// There seems to be no better way to find out which predicate we are in
1826-
.find(|pred| pred.span().contains(obligation_span))
1827-
.map(|pred| pred.span())
1828-
.unwrap_or(obligation_span);
1829-
}
18301832

18311833
let obligation = traits::Obligation::new(
18321834
traits::ObligationCause::new(span, id, traits::TrivialBound),
18331835
empty_env,
18341836
pred,
18351837
);
18361838
fcx.register_predicate(obligation);
1839+
} else {
1840+
let infer::InferOk { value: norm_pred, obligations } =
1841+
fcx.normalize_associated_types_in_as_infer_ok(span, pred);
1842+
if norm_pred.is_global() {
1843+
fcx.probe(|_| {
1844+
let mut fulfillment_cx = traits::FulfillmentContext::new_in_snapshot();
1845+
for obligation in obligations {
1846+
fulfillment_cx.register_predicate_obligation(&fcx, obligation);
1847+
}
1848+
fulfillment_cx.register_predicate_obligation(&fcx, traits::Obligation::new(
1849+
traits::ObligationCause::new(span, id, traits::TrivialBound),
1850+
empty_env,
1851+
norm_pred,
1852+
));
1853+
if !fulfillment_cx.select_all_or_error(&fcx).is_empty() {
1854+
let mut diag = fcx
1855+
.tcx
1856+
.sess
1857+
// NOTE: Error for the crater run
1858+
.struct_span_warn(span, "where-clause bound is impossible to satisfy");
1859+
diag.note(format!("the bound `{}` was previously accepted, but it may become a hard error in a future release", pred));
1860+
if fcx.sess().opts.unstable_features.is_nightly_build() {
1861+
diag.help(
1862+
"add `#![feature(trivial_bounds)]` to the crate attributes to allow it",
1863+
);
1864+
}
1865+
diag.emit();
1866+
}
1867+
})
1868+
}
18371869
}
18381870
}
18391871

0 commit comments

Comments
 (0)