Skip to content

Commit c9fcbda

Browse files
committed
check where clause before suggesting unsized
1 parent b053550 commit c9fcbda

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::infer::{self, InferCtxt, TyCtxtInferExt};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
1616
use rustc_hir as hir;
17+
use rustc_hir::def::{DefKind, Res};
1718
use rustc_hir::def_id::DefId;
1819
use rustc_hir::intravisit::Visitor;
1920
use rustc_hir::GenericParam;
@@ -2009,6 +2010,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
20092010
Some(param) => param,
20102011
_ => return,
20112012
};
2013+
let param_def_id = self.tcx.hir().local_def_id(param.hir_id).to_def_id();
2014+
let preds = generics.where_clause.predicates.iter();
2015+
let explicitly_sized = preds
2016+
.filter_map(|pred| match pred {
2017+
hir::WherePredicate::BoundPredicate(bp) => Some(bp),
2018+
_ => None,
2019+
})
2020+
.flat_map(|bp| match bp.bounded_ty.kind {
2021+
hir::TyKind::Path(hir::QPath::Resolved(
2022+
None,
2023+
&hir::Path { res: Res::Def(DefKind::TyParam, def_id), .. },
2024+
)) if def_id == param_def_id => bp.bounds,
2025+
_ => &[][..],
2026+
})
2027+
.any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
2028+
if explicitly_sized {
2029+
return;
2030+
}
20122031
debug!("maybe_suggest_unsized_generics: param={:?}", param);
20132032
match node {
20142033
hir::Node::Item(
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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)