Skip to content

Commit 7a5d2d0

Browse files
committed
Auto merge of #114358 - matthiaskrgr:rollup-d810m9e, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #114178 (Account for macros when suggesting a new let binding) - #114199 (Don't unsize coerce infer vars in select in new solver) - #114301 (Don't check unnecessarily that impl trait is RPIT) - #114314 (Tweaks to `adt_sized_constraint`) - #114322 (Fix invalid slice coercion suggestion reported in turbofish) - #114340 ([rustc_attr][nit] Replace `filter` + `is_some` with `map_or`.) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 90bb418 + 4876afb commit 7a5d2d0

File tree

25 files changed

+168
-69
lines changed

25 files changed

+168
-69
lines changed

compiler/rustc_attr/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn rust_version_symbol() -> Symbol {
2828
}
2929

3030
pub fn is_builtin_attr(attr: &Attribute) -> bool {
31-
attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
31+
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
3232
}
3333

3434
enum AttrError {

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2133,13 +2133,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21332133
self.current -= 1;
21342134
}
21352135
fn visit_expr(&mut self, expr: &hir::Expr<'tcx>) {
2136-
if self.span == expr.span {
2136+
if self.span == expr.span.source_callsite() {
21372137
self.found = self.current;
21382138
}
21392139
walk_expr(self, expr);
21402140
}
21412141
}
21422142
let source_info = self.body.source_info(location);
2143+
let proper_span = proper_span.source_callsite();
21432144
if let Some(scope) = self.body.source_scopes.get(source_info.scope)
21442145
&& let ClearCrossCrate::Set(scope_data) = &scope.local_data
21452146
&& let Some(node) = self.infcx.tcx.hir().find(scope_data.lint_root)

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14621462
let traits: Vec<_> =
14631463
self.probe_traits_that_match_assoc_ty(qself_ty, assoc_ident);
14641464

1465-
// Don't print `TyErr` to the user.
1465+
// Don't print `ty::Error` to the user.
14661466
self.report_ambiguous_associated_type(
14671467
span,
14681468
&[qself_ty.to_string()],

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
555555
for (region_a, region_a_idx) in &regions {
556556
// Ignore `'static` lifetimes for the purpose of this lint: it's
557557
// because we know it outlives everything and so doesn't give meaningful
558-
// clues
559-
if let ty::ReStatic = **region_a {
558+
// clues. Also ignore `ReError`, to avoid knock-down errors.
559+
if let ty::ReStatic | ty::ReError(_) = **region_a {
560560
continue;
561561
}
562562
// For each region argument (e.g., `'a` in our example), check for a
@@ -599,8 +599,9 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
599599
// on the GAT itself.
600600
for (region_b, region_b_idx) in &regions {
601601
// Again, skip `'static` because it outlives everything. Also, we trivially
602-
// know that a region outlives itself.
603-
if ty::ReStatic == **region_b || region_a == region_b {
602+
// know that a region outlives itself. Also ignore `ReError`, to avoid
603+
// knock-down errors.
604+
if matches!(**region_b, ty::ReStatic | ty::ReError(_)) || region_a == region_b {
604605
continue;
605606
}
606607
if region_known_to_outlive(

compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
795795

796796
/// Converts the types that the user supplied, in case that doing
797797
/// so should yield an error, but returns back a signature where
798-
/// all parameters are of type `TyErr`.
798+
/// all parameters are of type `ty::Error`.
799799
fn error_sig_of_closure(
800800
&self,
801801
decl: &hir::FnDecl<'_>,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2567,15 +2567,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25672567
None,
25682568
);
25692569
if let Some(infer::RelateParamBound(_, t, _)) = origin {
2570-
let return_impl_trait =
2571-
self.tcx.return_type_impl_trait(generic_param_scope).is_some();
25722570
let t = self.resolve_vars_if_possible(t);
25732571
match t.kind() {
25742572
// We've got:
25752573
// fn get_later<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
25762574
// suggest:
25772575
// fn get_later<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
2578-
ty::Closure(..) | ty::Alias(ty::Opaque, ..) if return_impl_trait => {
2576+
ty::Closure(..) | ty::Alias(ty::Opaque, ..) => {
25792577
new_binding_suggestion(&mut err, type_param_span);
25802578
}
25812579
_ => {

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,10 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
942942
generic_ty: Ty<'tcx>,
943943
min: ty::Region<'tcx>,
944944
) -> bool {
945+
if let ty::ReError(_) = *min {
946+
return true;
947+
}
948+
945949
match bound {
946950
VerifyBound::IfEq(verify_if_eq_b) => {
947951
let verify_if_eq_b = var_values.normalize(self.region_rels.tcx, *verify_if_eq_b);

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ rustc_queries! {
706706
separate_provide_extern
707707
}
708708

709-
query adt_sized_constraint(key: DefId) -> &'tcx [Ty<'tcx>] {
709+
query adt_sized_constraint(key: DefId) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
710710
desc { |tcx| "computing `Sized` constraints for `{}`", tcx.def_path_str(key) }
711711
}
712712

compiler/rustc_middle/src/ty/adt.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -562,18 +562,10 @@ impl<'tcx> AdtDef<'tcx> {
562562
tcx.adt_destructor(self.did())
563563
}
564564

565-
/// Returns a list of types such that `Self: Sized` if and only
566-
/// if that type is `Sized`, or `TyErr` if this type is recursive.
567-
///
568-
/// Oddly enough, checking that the sized-constraint is `Sized` is
569-
/// actually more expressive than checking all members:
570-
/// the `Sized` trait is inductive, so an associated type that references
571-
/// `Self` would prevent its containing ADT from being `Sized`.
572-
///
573-
/// Due to normalization being eager, this applies even if
574-
/// the associated type is behind a pointer (e.g., issue #31299).
575-
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx [Ty<'tcx>]> {
576-
ty::EarlyBinder::bind(tcx.adt_sized_constraint(self.did()))
565+
/// Returns a list of types such that `Self: Sized` if and only if that
566+
/// type is `Sized`, or `ty::Error` if this type has a recursive layout.
567+
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
568+
tcx.adt_sized_constraint(self.did())
577569
}
578570
}
579571

compiler/rustc_middle/src/ty/context.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
5050
use rustc_hir::definitions::Definitions;
5151
use rustc_hir::intravisit::Visitor;
5252
use rustc_hir::lang_items::LangItem;
53-
use rustc_hir::{
54-
Constness, ExprKind, HirId, ImplItemKind, ItemKind, Node, TraitCandidate, TraitItemKind,
55-
};
53+
use rustc_hir::{Constness, HirId, Node, TraitCandidate};
5654
use rustc_index::IndexVec;
5755
use rustc_macros::HashStable;
5856
use rustc_query_system::dep_graph::DepNodeIndex;
@@ -1077,31 +1075,6 @@ impl<'tcx> TyCtxt<'tcx> {
10771075
return None;
10781076
}
10791077

1080-
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
1081-
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
1082-
match self.hir().get_by_def_id(scope_def_id) {
1083-
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
1084-
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
1085-
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
1086-
Node::Expr(&hir::Expr { kind: ExprKind::Closure { .. }, .. }) => {}
1087-
_ => return None,
1088-
}
1089-
1090-
let ret_ty = self.type_of(scope_def_id).instantiate_identity();
1091-
match ret_ty.kind() {
1092-
ty::FnDef(_, _) => {
1093-
let sig = ret_ty.fn_sig(self);
1094-
let output = self.erase_late_bound_regions(sig.output());
1095-
output.is_impl_trait().then(|| {
1096-
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
1097-
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
1098-
(output, fn_decl.output.span())
1099-
})
1100-
}
1101-
_ => None,
1102-
}
1103-
}
1104-
11051078
/// Checks if the bound region is in Impl Item.
11061079
pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool {
11071080
let container_id = self.parent(suitable_region_binding_scope.to_def_id());

compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub(in crate::solve) fn instantiate_constituent_tys_for_sized_trait<'tcx>(
150150

151151
ty::Adt(def, args) => {
152152
let sized_crit = def.sized_constraint(ecx.tcx());
153-
Ok(sized_crit.iter_instantiated_copied(ecx.tcx(), args).collect())
153+
Ok(sized_crit.iter_instantiated(ecx.tcx(), args).collect())
154154
}
155155
}
156156
}

compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs

+3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ fn rematch_unsize<'tcx>(
235235
goal.param_env,
236236
&mut nested,
237237
);
238+
238239
match (a_ty.kind(), b_ty.kind()) {
240+
// Don't try to coerce `?0` to `dyn Trait`
241+
(ty::Infer(ty::TyVar(_)), _) | (_, ty::Infer(ty::TyVar(_))) => Ok(None),
239242
// Stall any ambiguous upcasting goals, since we can't rematch those
240243
(ty::Dynamic(_, _, ty::Dyn), ty::Dynamic(_, _, ty::Dyn)) => match certainty {
241244
Certainty::Yes => Ok(Some(ImplSource::Builtin(source, nested))),

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30303030
self.report_similar_impl_candidates_for_root_obligation(&obligation, *trait_predicate, body_def_id, err);
30313031
}
30323032

3033-
self.maybe_suggest_convert_to_slice(
3033+
self.suggest_convert_to_slice(
30343034
err,
3035+
obligation,
30353036
trait_ref,
30363037
impl_candidates.as_slice(),
30373038
span,

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ pub trait TypeErrCtxtExt<'tcx> {
398398
param_env: ty::ParamEnv<'tcx>,
399399
) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>>;
400400

401-
fn maybe_suggest_convert_to_slice(
401+
fn suggest_convert_to_slice(
402402
&self,
403403
err: &mut Diagnostic,
404+
obligation: &PredicateObligation<'tcx>,
404405
trait_ref: ty::PolyTraitRef<'tcx>,
405406
candidate_impls: &[ImplCandidate<'tcx>],
406407
span: Span,
@@ -3944,13 +3945,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
39443945
/// If the type that failed selection is an array or a reference to an array,
39453946
/// but the trait is implemented for slices, suggest that the user converts
39463947
/// the array into a slice.
3947-
fn maybe_suggest_convert_to_slice(
3948+
fn suggest_convert_to_slice(
39483949
&self,
39493950
err: &mut Diagnostic,
3951+
obligation: &PredicateObligation<'tcx>,
39503952
trait_ref: ty::PolyTraitRef<'tcx>,
39513953
candidate_impls: &[ImplCandidate<'tcx>],
39523954
span: Span,
39533955
) {
3956+
// We can only suggest the slice coersion for function arguments since the suggestion
3957+
// would make no sense in turbofish or call
3958+
let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else {
3959+
return;
3960+
};
3961+
39543962
// Three cases where we can make a suggestion:
39553963
// 1. `[T; _]` (array of T)
39563964
// 2. `&[T; _]` (reference to array of T)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20992099
Where(
21002100
obligation
21012101
.predicate
2102-
.rebind(sized_crit.iter_instantiated_copied(self.tcx(), args).collect()),
2102+
.rebind(sized_crit.iter_instantiated(self.tcx(), args).collect()),
21032103
)
21042104
}
21052105

compiler/rustc_ty_utils/src/ty.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn sized_constraint_for_ty<'tcx>(
4242
let adt_tys = adt.sized_constraint(tcx);
4343
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
4444
adt_tys
45-
.iter_instantiated_copied(tcx, args)
45+
.iter_instantiated(tcx, args)
4646
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
4747
.collect()
4848
}
@@ -58,11 +58,18 @@ fn sized_constraint_for_ty<'tcx>(
5858
// we know that `T` is Sized and do not need to check
5959
// it on the impl.
6060

61-
let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] };
62-
let sized_predicate =
63-
ty::TraitRef::new(tcx, sized_trait, [ty]).without_const().to_predicate(tcx);
61+
let Some(sized_trait_def_id) = tcx.lang_items().sized_trait() else { return vec![ty] };
6462
let predicates = tcx.predicates_of(adtdef.did()).predicates;
65-
if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] }
63+
if predicates.iter().any(|(p, _)| {
64+
p.as_trait_clause().is_some_and(|trait_pred| {
65+
trait_pred.def_id() == sized_trait_def_id
66+
&& trait_pred.self_ty().skip_binder() == ty
67+
})
68+
}) {
69+
vec![]
70+
} else {
71+
vec![ty]
72+
}
6673
}
6774

6875
Placeholder(..) | Bound(..) | Infer(..) => {
@@ -92,10 +99,13 @@ fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness {
9299
/// - a tuple of type parameters or projections, if there are multiple
93100
/// such.
94101
/// - an Error, if a type is infinitely sized
95-
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
102+
fn adt_sized_constraint<'tcx>(
103+
tcx: TyCtxt<'tcx>,
104+
def_id: DefId,
105+
) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
96106
if let Some(def_id) = def_id.as_local() {
97107
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
98-
return tcx.mk_type_list(&[Ty::new_misc_error(tcx)]);
108+
return ty::EarlyBinder::bind(tcx.mk_type_list(&[Ty::new_misc_error(tcx)]));
99109
}
100110
}
101111
let def = tcx.adt_def(def_id);
@@ -107,7 +117,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
107117

108118
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
109119

110-
result
120+
ty::EarlyBinder::bind(result)
111121
}
112122

113123
/// See `ParamEnv` struct definition for details.

tests/ui/borrowck/borrowck-borrowed-uniq-rvalue-2.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ LL | let x = defer(&vec!["Goodbye", "world!"]);
88
LL | x.x[0];
99
| ------ borrow later used here
1010
|
11-
= note: consider using a `let` binding to create a longer lived value
1211
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
help: consider using a `let` binding to create a longer lived value
13+
|
14+
LL ~ let binding = vec!["Goodbye", "world!"];
15+
LL ~ let x = defer(&binding);
16+
|
1317

1418
error: aborting due to previous error
1519

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Test {}
2+
impl Test for &[u8] {}
3+
4+
fn needs_test<T: Test>() -> T {
5+
panic!()
6+
}
7+
8+
fn main() {
9+
needs_test::<[u8; 1]>();
10+
//~^ ERROR the trait bound
11+
let x: [u8; 1] = needs_test();
12+
//~^ ERROR the trait bound
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
2+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:9:18
3+
|
4+
LL | needs_test::<[u8; 1]>();
5+
| ^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
6+
|
7+
= help: the trait `Test` is implemented for `&[u8]`
8+
note: required by a bound in `needs_test`
9+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:4:18
10+
|
11+
LL | fn needs_test<T: Test>() -> T {
12+
| ^^^^ required by this bound in `needs_test`
13+
14+
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
15+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:11:22
16+
|
17+
LL | let x: [u8; 1] = needs_test();
18+
| ^^^^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
19+
|
20+
= help: the trait `Test` is implemented for `&[u8]`
21+
note: required by a bound in `needs_test`
22+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:4:18
23+
|
24+
LL | fn needs_test<T: Test>() -> T {
25+
| ^^^^ required by this bound in `needs_test`
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(return_position_impl_trait_in_trait)]
2+
3+
trait Iterable {
4+
type Item<'a>
5+
where
6+
Self: 'a;
7+
8+
fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
9+
//~^ ERROR use of undeclared lifetime name `'missing`
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0261]: use of undeclared lifetime name `'missing`
2+
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:8:55
3+
|
4+
LL | fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
5+
| ^^^^^^^^ undeclared lifetime
6+
|
7+
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
8+
help: consider making the bound lifetime-generic with a new `'missing` lifetime
9+
|
10+
LL | fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>>;
11+
| +++++++++++++
12+
help: consider introducing lifetime `'missing` here
13+
|
14+
LL | fn iter<'missing>(&self) -> impl Iterator<Item = Self::Item<'missing>>;
15+
| ++++++++++
16+
help: consider introducing lifetime `'missing` here
17+
|
18+
LL | trait Iterable<'missing> {
19+
| ++++++++++
20+
21+
error: aborting due to previous error
22+
23+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)