Skip to content

Commit ebef3ce

Browse files
authored
Rollup merge of #86455 - tlyu:check-where-before-suggesting-unsized, r=estebank
check where-clause for explicit `Sized` before suggesting `?Sized` Fixes #85945. Based on #86454. ``@rustbot`` label +A-diagnostics +A-traits +A-typesystem +D-papercut +T-compiler
2 parents 0206312 + 1a50725 commit ebef3ce

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

compiler/rustc_hir/src/hir.rs

+16
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,22 @@ pub struct WhereBoundPredicate<'hir> {
647647
pub bounds: GenericBounds<'hir>,
648648
}
649649

650+
impl WhereBoundPredicate<'hir> {
651+
/// Returns `true` if `param_def_id` matches the `bounded_ty` of this predicate.
652+
pub fn is_param_bound(&self, param_def_id: DefId) -> bool {
653+
let path = match self.bounded_ty.kind {
654+
TyKind::Path(QPath::Resolved(None, path)) => path,
655+
_ => return false,
656+
};
657+
match path.res {
658+
Res::Def(DefKind::TyParam, def_id) | Res::SelfTy(Some(def_id), None) => {
659+
def_id == param_def_id
660+
}
661+
_ => false,
662+
}
663+
}
664+
}
665+
650666
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
651667
#[derive(Debug, HashStable_Generic)]
652668
pub struct WhereRegionPredicate<'hir> {

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

+13
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,19 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
20092009
Some(param) => param,
20102010
_ => return,
20112011
};
2012+
let param_def_id = self.tcx.hir().local_def_id(param.hir_id).to_def_id();
2013+
let preds = generics.where_clause.predicates.iter();
2014+
let explicitly_sized = preds
2015+
.filter_map(|pred| match pred {
2016+
hir::WherePredicate::BoundPredicate(bp) => Some(bp),
2017+
_ => None,
2018+
})
2019+
.filter(|bp| bp.is_param_bound(param_def_id))
2020+
.flat_map(|bp| bp.bounds)
2021+
.any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
2022+
if explicitly_sized {
2023+
return;
2024+
}
20122025
debug!("maybe_suggest_unsized_generics: param={:?}", param);
20132026
match node {
20142027
hir::Node::Item(

compiler/rustc_typeck/src/collect.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_data_structures::captures::Captures;
2828
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
2929
use rustc_errors::{struct_span_err, Applicability};
3030
use rustc_hir as hir;
31-
use rustc_hir::def::{CtorKind, DefKind, Res};
31+
use rustc_hir::def::{CtorKind, DefKind};
3232
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
3333
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
3434
use rustc_hir::weak_lang_items;
@@ -668,6 +668,7 @@ impl ItemCtxt<'tcx> {
668668
})
669669
.flat_map(|b| predicates_from_bound(self, ty, b));
670670

671+
let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id();
671672
let from_where_clauses = ast_generics
672673
.where_clause
673674
.predicates
@@ -677,7 +678,7 @@ impl ItemCtxt<'tcx> {
677678
_ => None,
678679
})
679680
.flat_map(|bp| {
680-
let bt = if is_param(self.tcx, bp.bounded_ty, param_id) {
681+
let bt = if bp.is_param_bound(param_def_id) {
681682
Some(ty)
682683
} else if !only_self_bounds.0 {
683684
Some(self.to_ty(bp.bounded_ty))
@@ -714,23 +715,6 @@ impl ItemCtxt<'tcx> {
714715
}
715716
}
716717

717-
/// Tests whether this is the AST for a reference to the type
718-
/// parameter with ID `param_id`. We use this so as to avoid running
719-
/// `ast_ty_to_ty`, because we want to avoid triggering an all-out
720-
/// conversion of the type to avoid inducing unnecessary cycles.
721-
fn is_param(tcx: TyCtxt<'_>, ast_ty: &hir::Ty<'_>, param_id: hir::HirId) -> bool {
722-
if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ast_ty.kind {
723-
match path.res {
724-
Res::SelfTy(Some(def_id), None) | Res::Def(DefKind::TyParam, def_id) => {
725-
def_id == tcx.hir().local_def_id(param_id).to_def_id()
726-
}
727-
_ => false,
728-
}
729-
} else {
730-
false
731-
}
732-
}
733-
734718
fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
735719
let it = tcx.hir().item(item_id);
736720
debug!("convert: item {} with id {}", it.ident, it.hir_id());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for #85945: Don't suggest `?Sized` bound if an explicit
2+
// `Sized` bound is already in a `where` clause.
3+
fn foo<T>(_: &T) where T: Sized {}
4+
fn bar() { foo(""); }
5+
//~^ERROR the size for values of type
6+
7+
pub fn main() {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/issue-85945-check-where-clause-before-suggesting-unsized.rs:4:16
3+
|
4+
LL | fn bar() { foo(""); }
5+
| --- ^^ doesn't have a size known at compile-time
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `Sized` is not implemented for `str`
10+
note: required by a bound in `foo`
11+
--> $DIR/issue-85945-check-where-clause-before-suggesting-unsized.rs:3:8
12+
|
13+
LL | fn foo<T>(_: &T) where T: Sized {}
14+
| ^ required by this bound in `foo`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)