Skip to content

Commit c8813dd

Browse files
committed
Auto merge of rust-lang#122607 - fmease:rollup-ozl1eeq, r=fmease
Rollup of 9 pull requests Successful merges: - rust-lang#117918 (Add `wasm_c_abi` `future-incompat` lint) - rust-lang#121545 (fix attribute validation on associated items in traits) - rust-lang#121720 (Split refining_impl_trait lint into _reachable, _internal variants) - rust-lang#122270 (fix `long-linker-command-lines` failure caused by `rust.rpath=false`) - rust-lang#122564 (Delegation: fix ICE on duplicated associative items) - rust-lang#122577 (Remove obsolete parameter `speculative` from `instantiate_poly_trait_ref`) - rust-lang#122601 (Optimize `ptr::replace`) - rust-lang#122604 (Mention jieyouxu for changes to compiletest, run-make tests and the run-make-support library) - rust-lang#122605 (rustc-metadata: Store crate name in self-profile of metadata_register_crate) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4c1b9c3 + caa6131 commit c8813dd

File tree

27 files changed

+454
-104
lines changed

27 files changed

+454
-104
lines changed

compiler/rustc_ast_lowering/src/item.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
373373
(trait_ref, lowered_ty)
374374
});
375375

376+
self.is_in_trait_impl = trait_ref.is_some();
376377
let new_impl_items = self
377378
.arena
378379
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
@@ -978,13 +979,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
978979
}
979980

980981
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
981-
let trait_item_def_id = self
982-
.resolver
983-
.get_partial_res(i.id)
984-
.map(|r| r.expect_full_res().opt_def_id())
985-
.unwrap_or(None);
986-
self.is_in_trait_impl = trait_item_def_id.is_some();
987-
988982
hir::ImplItemRef {
989983
id: hir::ImplItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
990984
ident: self.lower_ident(i.ident),
@@ -1000,7 +994,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1000994
},
1001995
AssocItemKind::MacCall(..) => unimplemented!(),
1002996
},
1003-
trait_item_def_id,
997+
trait_item_def_id: self
998+
.resolver
999+
.get_partial_res(i.id)
1000+
.map(|r| r.expect_full_res().opt_def_id())
1001+
.unwrap_or(None),
10041002
}
10051003
}
10061004

compiler/rustc_ast_passes/src/ast_validation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15271527
generics,
15281528
body.as_deref(),
15291529
);
1530+
walk_list!(self, visit_attribute, &item.attrs);
15301531
self.visit_fn(kind, item.span, item.id);
15311532
}
15321533
AssocItemKind::Type(_) => {

compiler/rustc_hir_analysis/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ hir_analysis_rpitit_refined = impl trait in impl method signature does not match
351351
.label = return type from trait method defined here
352352
.unmatched_bound_label = this bound is stronger than that defined on the trait
353353
.note = add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
354+
.feedback_note = we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
354355
355356
hir_analysis_self_in_impl_self =
356357
`Self` is not valid in the self type of an impl block

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+47-54
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
149149
polarity,
150150
param_ty,
151151
bounds,
152-
false,
153152
only_self_bounds,
154153
);
155154
}
@@ -231,14 +230,13 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
231230
/// **A note on binders:** given something like `T: for<'a> Iterator<Item = &'a u32>`, the
232231
/// `trait_ref` here will be `for<'a> T: Iterator`. The `binding` data however is from *inside*
233232
/// the binder (e.g., `&'a u32`) and hence may reference bound regions.
234-
#[instrument(level = "debug", skip(self, bounds, speculative, dup_bindings, path_span))]
233+
#[instrument(level = "debug", skip(self, bounds, dup_bindings, path_span))]
235234
pub(super) fn add_predicates_for_ast_type_binding(
236235
&self,
237236
hir_ref_id: hir::HirId,
238237
trait_ref: ty::PolyTraitRef<'tcx>,
239238
binding: &hir::TypeBinding<'tcx>,
240239
bounds: &mut Bounds<'tcx>,
241-
speculative: bool,
242240
dup_bindings: &mut FxIndexMap<DefId, Span>,
243241
path_span: Span,
244242
only_self_bounds: OnlySelfBounds,
@@ -317,19 +315,17 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
317315
}
318316
tcx.check_stability(assoc_item.def_id, Some(hir_ref_id), binding.span, None);
319317

320-
if !speculative {
321-
dup_bindings
322-
.entry(assoc_item.def_id)
323-
.and_modify(|prev_span| {
324-
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
325-
span: binding.span,
326-
prev_span: *prev_span,
327-
item_name: binding.ident,
328-
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
329-
});
330-
})
331-
.or_insert(binding.span);
332-
}
318+
dup_bindings
319+
.entry(assoc_item.def_id)
320+
.and_modify(|prev_span| {
321+
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
322+
span: binding.span,
323+
prev_span: *prev_span,
324+
item_name: binding.ident,
325+
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
326+
});
327+
})
328+
.or_insert(binding.span);
333329

334330
let projection_ty = if let ty::AssocKind::Fn = assoc_kind {
335331
let mut emitted_bad_param_err = None;
@@ -433,9 +429,8 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
433429
});
434430

435431
// Provide the resolved type of the associated constant to `type_of(AnonConst)`.
436-
if !speculative
437-
&& let hir::TypeBindingKind::Equality { term: hir::Term::Const(anon_const) } =
438-
binding.kind
432+
if let hir::TypeBindingKind::Equality { term: hir::Term::Const(anon_const) } =
433+
binding.kind
439434
{
440435
let ty = alias_ty.map_bound(|ty| tcx.type_of(ty.def_id).instantiate(tcx, ty.args));
441436
// Since the arguments passed to the alias type above may contain early-bound
@@ -463,42 +458,40 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
463458
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(),
464459
};
465460

466-
if !speculative {
467-
// Find any late-bound regions declared in `ty` that are not
468-
// declared in the trait-ref or assoc_item. These are not well-formed.
469-
//
470-
// Example:
471-
//
472-
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
473-
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
474-
let late_bound_in_projection_ty =
475-
tcx.collect_constrained_late_bound_regions(projection_ty);
476-
let late_bound_in_term =
477-
tcx.collect_referenced_late_bound_regions(trait_ref.rebind(term));
478-
debug!(?late_bound_in_projection_ty);
479-
debug!(?late_bound_in_term);
461+
// Find any late-bound regions declared in `ty` that are not
462+
// declared in the trait-ref or assoc_item. These are not well-formed.
463+
//
464+
// Example:
465+
//
466+
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
467+
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
468+
let late_bound_in_projection_ty =
469+
tcx.collect_constrained_late_bound_regions(projection_ty);
470+
let late_bound_in_term =
471+
tcx.collect_referenced_late_bound_regions(trait_ref.rebind(term));
472+
debug!(?late_bound_in_projection_ty);
473+
debug!(?late_bound_in_term);
480474

481-
// FIXME: point at the type params that don't have appropriate lifetimes:
482-
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
483-
// ---- ---- ^^^^^^^
484-
// NOTE(associated_const_equality): This error should be impossible to trigger
485-
// with associated const equality bounds.
486-
self.validate_late_bound_regions(
487-
late_bound_in_projection_ty,
488-
late_bound_in_term,
489-
|br_name| {
490-
struct_span_code_err!(
491-
tcx.dcx(),
492-
binding.span,
493-
E0582,
494-
"binding for associated type `{}` references {}, \
495-
which does not appear in the trait input types",
496-
binding.ident,
497-
br_name
498-
)
499-
},
500-
);
501-
}
475+
// FIXME: point at the type params that don't have appropriate lifetimes:
476+
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
477+
// ---- ---- ^^^^^^^
478+
// NOTE(associated_const_equality): This error should be impossible to trigger
479+
// with associated const equality bounds.
480+
self.validate_late_bound_regions(
481+
late_bound_in_projection_ty,
482+
late_bound_in_term,
483+
|br_name| {
484+
struct_span_code_err!(
485+
tcx.dcx(),
486+
binding.span,
487+
E0582,
488+
"binding for associated type `{}` references {}, \
489+
which does not appear in the trait input types",
490+
binding.ident,
491+
br_name
492+
)
493+
},
494+
);
502495

503496
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
504497
// the "projection predicate" for:

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
639639
/// where `'a` is a bound region at depth 0. Similarly, the `poly_trait_ref` would be
640640
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
641641
/// however.
642-
#[instrument(level = "debug", skip(self, span, constness, bounds, speculative))]
642+
#[instrument(level = "debug", skip(self, span, constness, bounds))]
643643
pub(crate) fn instantiate_poly_trait_ref(
644644
&self,
645645
trait_ref: &hir::TraitRef<'tcx>,
@@ -648,7 +648,6 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
648648
polarity: ty::ImplPolarity,
649649
self_ty: Ty<'tcx>,
650650
bounds: &mut Bounds<'tcx>,
651-
speculative: bool,
652651
only_self_bounds: OnlySelfBounds,
653652
) -> GenericArgCountResult {
654653
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
@@ -697,7 +696,6 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
697696
poly_trait_ref,
698697
binding,
699698
bounds,
700-
speculative,
701699
&mut dup_bindings,
702700
binding.span,
703701
only_self_bounds,

compiler/rustc_hir_analysis/src/astconv/object_safety.rs

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
4444
ty::ImplPolarity::Positive,
4545
dummy_self,
4646
&mut bounds,
47-
false,
4847
// True so we don't populate `bounds` with associated type bounds, even
4948
// though they're disallowed from object types.
5049
OnlySelfBounds(true),

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxIndexSet;
22
use rustc_hir as hir;
33
use rustc_hir::def_id::DefId;
44
use rustc_infer::infer::{outlives::env::OutlivesEnvironment, TyCtxtInferExt};
5-
use rustc_lint_defs::builtin::REFINING_IMPL_TRAIT;
5+
use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT_REACHABLE};
66
use rustc_middle::traits::{ObligationCause, Reveal};
77
use rustc_middle::ty::{
88
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
@@ -23,26 +23,23 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
2323
if !tcx.impl_method_has_trait_impl_trait_tys(impl_m.def_id) {
2424
return;
2525
}
26+
2627
// unreachable traits don't have any library guarantees, there's no need to do this check.
27-
if trait_m
28+
let is_internal = trait_m
2829
.container_id(tcx)
2930
.as_local()
3031
.is_some_and(|trait_def_id| !tcx.effective_visibilities(()).is_reachable(trait_def_id))
31-
{
32-
return;
33-
}
32+
// If a type in the trait ref is private, then there's also no reason to do this check.
33+
|| impl_trait_ref.args.iter().any(|arg| {
34+
if let Some(ty) = arg.as_type()
35+
&& let Some(self_visibility) = type_visibility(tcx, ty)
36+
{
37+
return !self_visibility.is_public();
38+
}
39+
false
40+
});
3441

35-
// If a type in the trait ref is private, then there's also no reason to do this check.
3642
let impl_def_id = impl_m.container_id(tcx);
37-
for arg in impl_trait_ref.args {
38-
if let Some(ty) = arg.as_type()
39-
&& let Some(self_visibility) = type_visibility(tcx, ty)
40-
&& !self_visibility.is_public()
41-
{
42-
return;
43-
}
44-
}
45-
4643
let impl_m_args = ty::GenericArgs::identity_for_item(tcx, impl_m.def_id);
4744
let trait_m_to_impl_m_args = impl_m_args.rebase_onto(tcx, impl_def_id, impl_trait_ref.args);
4845
let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_m_to_impl_m_args);
@@ -85,6 +82,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
8582
trait_m.def_id,
8683
impl_m.def_id,
8784
None,
85+
is_internal,
8886
);
8987
return;
9088
};
@@ -104,6 +102,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
104102
trait_m.def_id,
105103
impl_m.def_id,
106104
None,
105+
is_internal,
107106
);
108107
return;
109108
}
@@ -198,6 +197,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
198197
trait_m.def_id,
199198
impl_m.def_id,
200199
Some(span),
200+
is_internal,
201201
);
202202
return;
203203
}
@@ -235,6 +235,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
235235
trait_m_def_id: DefId,
236236
impl_m_def_id: DefId,
237237
unmatched_bound: Option<Span>,
238+
is_internal: bool,
238239
) {
239240
let mapping = std::iter::zip(
240241
tcx.fn_sig(trait_m_def_id).skip_binder().bound_vars(),
@@ -287,7 +288,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
287288

288289
let span = unmatched_bound.unwrap_or(span);
289290
tcx.emit_node_span_lint(
290-
REFINING_IMPL_TRAIT,
291+
if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
291292
tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
292293
span,
293294
crate::errors::ReturnPositionImplTraitInTraitRefined {

compiler/rustc_hir_analysis/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ pub struct UnusedAssociatedTypeBounds {
10721072
#[derive(LintDiagnostic)]
10731073
#[diag(hir_analysis_rpitit_refined)]
10741074
#[note]
1075+
#[note(hir_analysis_feedback_note)]
10751076
pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> {
10761077
#[suggestion(applicability = "maybe-incorrect", code = "{pre}{return_ty}{post}")]
10771078
pub impl_return_span: Span,

compiler/rustc_lint/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ fn register_builtins(store: &mut LintStore) {
313313
// MACRO_USE_EXTERN_CRATE
314314
);
315315

316+
add_lint_group!(
317+
"refining_impl_trait",
318+
REFINING_IMPL_TRAIT_REACHABLE,
319+
REFINING_IMPL_TRAIT_INTERNAL
320+
);
321+
316322
// Register renamed and removed lints.
317323
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
318324
store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");

0 commit comments

Comments
 (0)