Skip to content

Commit b17226f

Browse files
committed
Auto merge of #94121 - matthiaskrgr:rollup-6ps95da, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #92683 (Suggest copying trait associated type bounds on lifetime error) - #92933 (Deny mixing bin crate type with lib crate types) - #92959 (Add more info and suggestions to use of #[test] on invalid items) - #93024 (Do not ICE when inlining a function with un-satisfiable bounds) - #93613 (Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`) - #93634 (compiler: clippy::complexity fixes) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents b8c56fa + a144ea1 commit b17226f

File tree

59 files changed

+613
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+613
-221
lines changed

compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl AttrItem {
230230
}
231231

232232
pub fn meta_kind(&self) -> Option<MetaItemKind> {
233-
Some(MetaItemKind::from_mac_args(&self.args)?)
233+
MetaItemKind::from_mac_args(&self.args)
234234
}
235235
}
236236

compiler/rustc_ast_passes/src/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
823823
);
824824
let mut all_stable = true;
825825
for ident in
826-
attr.meta_item_list().into_iter().flatten().map(|nested| nested.ident()).flatten()
826+
attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident())
827827
{
828828
let name = ident.name;
829829
let stable_since = lang_features

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ pub fn inject(
5656
is_proc_macro_crate: bool,
5757
has_proc_macro_decls: bool,
5858
is_test_crate: bool,
59-
num_crate_types: usize,
6059
handler: &rustc_errors::Handler,
6160
) -> ast::Crate {
6261
let ecfg = ExpansionConfig::default("proc_macro".to_string());
@@ -81,10 +80,6 @@ pub fn inject(
8180
return krate;
8281
}
8382

84-
if num_crate_types > 1 {
85-
handler.err("cannot mix `proc-macro` crate type with others");
86-
}
87-
8883
if is_test_crate {
8984
return krate;
9085
}

compiler/rustc_builtin_macros/src/test.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast as ast;
66
use rustc_ast::attr;
77
use rustc_ast::ptr::P;
88
use rustc_ast_pretty::pprust;
9+
use rustc_errors::Applicability;
910
use rustc_expand::base::*;
1011
use rustc_session::Session;
1112
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -102,11 +103,20 @@ pub fn expand_test_or_bench(
102103
}
103104
};
104105

105-
if let ast::ItemKind::MacCall(_) = item.kind {
106-
cx.sess.parse_sess.span_diagnostic.span_warn(
107-
item.span,
108-
"`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.",
109-
);
106+
// Note: non-associated fn items are already handled by `expand_test_or_bench`
107+
if !matches!(item.kind, ast::ItemKind::Fn(_)) {
108+
cx.sess
109+
.parse_sess
110+
.span_diagnostic
111+
.struct_span_err(
112+
attr_sp,
113+
"the `#[test]` attribute may only be used on a non-associated function",
114+
)
115+
.note("the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions")
116+
.span_label(item.span, format!("expected a non-associated function, found {} {}", item.kind.article(), item.kind.descr()))
117+
.span_suggestion(attr_sp, "replace with conditional compilation to make the item only exist when tests are being run", String::from("#[cfg(test)]"), Applicability::MaybeIncorrect)
118+
.emit();
119+
110120
return vec![Annotatable::Item(item)];
111121
}
112122

@@ -466,7 +476,7 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
466476
(false, _) => true,
467477
}
468478
} else {
469-
sd.span_err(i.span, "only functions may be used as tests");
479+
// should be unreachable because `is_test_fn_item` should catch all non-fn items
470480
false
471481
}
472482
}

compiler/rustc_codegen_llvm/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub unsafe fn create_module<'ll>(
292292
"sign-return-address-all\0".as_ptr().cast(),
293293
pac_opts.leaf.into(),
294294
);
295-
let is_bkey = if pac_opts.key == PAuthKey::A { false } else { true };
295+
let is_bkey: bool = pac_opts.key != PAuthKey::A;
296296
llvm::LLVMRustAddModuleFlag(
297297
llmod,
298298
llvm::LLVMModFlagBehavior::Error,

compiler/rustc_codegen_ssa/src/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
476476
mir::ProjectionElem::Subslice { from, to, from_end } => {
477477
let mut subslice = cg_base.project_index(bx, bx.cx().const_usize(from as u64));
478478
let projected_ty =
479-
PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx, elem.clone()).ty;
479+
PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx, *elem).ty;
480480
subslice.layout = bx.cx().layout_of(self.monomorphize(projected_ty));
481481

482482
if subslice.layout.is_unsized() {

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ fn check_matcher_core(
10391039
));
10401040
err.span_suggestion(
10411041
span,
1042-
&format!("try a `pat_param` fragment specifier instead"),
1042+
"try a `pat_param` fragment specifier instead",
10431043
suggestion,
10441044
Applicability::MaybeIncorrect,
10451045
);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
6464
.or_else(|| self.try_report_mismatched_static_lifetime())
6565
}
6666

67-
pub fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> {
67+
pub(super) fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> {
6868
match (&self.error, self.regions) {
6969
(Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), *sub, *sup)),
7070
(Some(SubSupConflict(_, _, origin, sub, _, sup, _)), None) => {

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

+52
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
102102
"...so that the definition in impl matches the definition from the trait",
103103
);
104104
}
105+
infer::CheckAssociatedTypeBounds { ref parent, .. } => {
106+
self.note_region_origin(err, &parent);
107+
}
105108
}
106109
}
107110

@@ -345,6 +348,55 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
345348
trait_item_def_id,
346349
&format!("`{}: {}`", sup, sub),
347350
),
351+
infer::CheckAssociatedTypeBounds { impl_item_def_id, trait_item_def_id, parent } => {
352+
let mut err = self.report_concrete_failure(*parent, sub, sup);
353+
354+
let trait_item_span = self.tcx.def_span(trait_item_def_id);
355+
let item_name = self.tcx.item_name(impl_item_def_id);
356+
err.span_label(
357+
trait_item_span,
358+
format!("definition of `{}` from trait", item_name),
359+
);
360+
361+
let trait_predicates = self.tcx.explicit_predicates_of(trait_item_def_id);
362+
let impl_predicates = self.tcx.explicit_predicates_of(impl_item_def_id);
363+
364+
let impl_predicates: rustc_data_structures::stable_set::FxHashSet<_> =
365+
impl_predicates.predicates.into_iter().map(|(pred, _)| pred).collect();
366+
let clauses: Vec<_> = trait_predicates
367+
.predicates
368+
.into_iter()
369+
.filter(|&(pred, _)| !impl_predicates.contains(pred))
370+
.map(|(pred, _)| format!("{}", pred))
371+
.collect();
372+
373+
if !clauses.is_empty() {
374+
let where_clause_span = self
375+
.tcx
376+
.hir()
377+
.get_generics(impl_item_def_id.expect_local())
378+
.unwrap()
379+
.where_clause
380+
.tail_span_for_suggestion();
381+
382+
let suggestion = format!(
383+
"{} {}",
384+
if !impl_predicates.is_empty() { "," } else { " where" },
385+
clauses.join(", "),
386+
);
387+
err.span_suggestion(
388+
where_clause_span,
389+
&format!(
390+
"try copying {} from the trait",
391+
if clauses.len() > 1 { "these clauses" } else { "this clause" }
392+
),
393+
suggestion,
394+
rustc_errors::Applicability::MaybeIncorrect,
395+
);
396+
}
397+
398+
err
399+
}
348400
}
349401
}
350402

compiler/rustc_infer/src/infer/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ pub enum SubregionOrigin<'tcx> {
438438
/// Comparing the signature and requirements of an impl associated type
439439
/// against the containing trait
440440
CompareImplTypeObligation { span: Span, impl_item_def_id: DefId, trait_item_def_id: DefId },
441+
442+
/// Checking that the bounds of a trait's associated type hold for a given impl
443+
CheckAssociatedTypeBounds {
444+
parent: Box<SubregionOrigin<'tcx>>,
445+
impl_item_def_id: DefId,
446+
trait_item_def_id: DefId,
447+
},
441448
}
442449

443450
// `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
@@ -1832,6 +1839,7 @@ impl<'tcx> SubregionOrigin<'tcx> {
18321839
ReferenceOutlivesReferent(_, a) => a,
18331840
CompareImplMethodObligation { span, .. } => span,
18341841
CompareImplTypeObligation { span, .. } => span,
1842+
CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
18351843
}
18361844
}
18371845

@@ -1862,6 +1870,15 @@ impl<'tcx> SubregionOrigin<'tcx> {
18621870
trait_item_def_id,
18631871
},
18641872

1873+
traits::ObligationCauseCode::CheckAssociatedTypeBounds {
1874+
impl_item_def_id,
1875+
trait_item_def_id,
1876+
} => SubregionOrigin::CheckAssociatedTypeBounds {
1877+
impl_item_def_id,
1878+
trait_item_def_id,
1879+
parent: Box::new(default()),
1880+
},
1881+
18651882
_ => default(),
18661883
}
18671884
}

compiler/rustc_interface/src/passes.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,18 @@ pub fn configure_and_expand(
393393
});
394394

395395
let crate_types = sess.crate_types();
396+
let is_executable_crate = crate_types.contains(&CrateType::Executable);
396397
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
397398

399+
if crate_types.len() > 1 {
400+
if is_executable_crate {
401+
sess.err("cannot mix `bin` crate type with others");
402+
}
403+
if is_proc_macro_crate {
404+
sess.err("cannot mix `proc-macro` crate type with others");
405+
}
406+
}
407+
398408
// For backwards compatibility, we don't try to run proc macro injection
399409
// if rustdoc is run on a proc macro crate without '--crate-type proc-macro' being
400410
// specified. This should only affect users who manually invoke 'rustdoc', as
@@ -411,7 +421,6 @@ pub fn configure_and_expand(
411421
msg.emit()
412422
} else {
413423
krate = sess.time("maybe_create_a_macro_crate", || {
414-
let num_crate_types = crate_types.len();
415424
let is_test_crate = sess.opts.test;
416425
rustc_builtin_macros::proc_macro_harness::inject(
417426
sess,
@@ -420,7 +429,6 @@ pub fn configure_and_expand(
420429
is_proc_macro_crate,
421430
has_proc_macro_decls,
422431
is_test_crate,
423-
num_crate_types,
424432
sess.diagnostic(),
425433
)
426434
});

compiler/rustc_middle/src/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn deprecation_message(
198198
} else {
199199
let since = since.as_ref().map(Symbol::as_str);
200200

201-
if since.as_deref() == Some("TBD") {
201+
if since == Some("TBD") {
202202
format!("use of {} `{}` that will be deprecated in a future Rust version", kind, path)
203203
} else {
204204
format!(

compiler/rustc_middle/src/traits/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ pub enum ObligationCauseCode<'tcx> {
285285
trait_item_def_id: DefId,
286286
},
287287

288+
/// Checking that the bounds of a trait's associated type hold for a given impl
289+
CheckAssociatedTypeBounds {
290+
impl_item_def_id: DefId,
291+
trait_item_def_id: DefId,
292+
},
293+
288294
/// Checking that this expression can be assigned where it needs to be
289295
// FIXME(eddyb) #11161 is the original Expr required?
290296
ExprAssignable,

compiler/rustc_middle/src/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
855855
) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>> {
856856
Ok(ty::ProjectionPredicate {
857857
projection_ty: relation.relate(a.projection_ty, b.projection_ty)?,
858-
term: relation.relate(a.term, b.term)?.into(),
858+
term: relation.relate(a.term, b.term)?,
859859
})
860860
}
861861
}

compiler/rustc_middle/src/ty/trait_def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<'tcx> TyCtxt<'tcx> {
210210
pub fn all_impls(self, def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
211211
let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(def_id);
212212

213-
blanket_impls.iter().chain(non_blanket_impls.iter().map(|(_, v)| v).flatten()).cloned()
213+
blanket_impls.iter().chain(non_blanket_impls.iter().flat_map(|(_, v)| v)).cloned()
214214
}
215215
}
216216

compiler/rustc_mir_transform/src/coverage/query.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn covered_code_regions<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Vec<&'tcx Cod
140140
let body = mir_body(tcx, def_id);
141141
body.basic_blocks()
142142
.iter()
143-
.map(|data| {
143+
.flat_map(|data| {
144144
data.statements.iter().filter_map(|statement| match statement.kind {
145145
StatementKind::Coverage(box ref coverage) => {
146146
if is_inlined(body, statement) {
@@ -152,7 +152,6 @@ fn covered_code_regions<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Vec<&'tcx Cod
152152
_ => None,
153153
})
154154
})
155-
.flatten()
156155
.collect()
157156
}
158157

compiler/rustc_mir_transform/src/inline.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_index::vec::Idx;
77
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
88
use rustc_middle::mir::visit::*;
99
use rustc_middle::mir::*;
10+
use rustc_middle::traits::ObligationCause;
1011
use rustc_middle::ty::subst::Subst;
1112
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
1213
use rustc_span::{hygiene::ExpnKind, ExpnData, Span};
@@ -75,10 +76,18 @@ fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
7576
return false;
7677
}
7778

79+
let param_env = tcx.param_env_reveal_all_normalized(def_id);
80+
let param_env = rustc_trait_selection::traits::normalize_param_env_or_error(
81+
tcx,
82+
def_id,
83+
param_env,
84+
ObligationCause::misc(body.span, hir_id),
85+
);
86+
7887
let mut this = Inliner {
7988
tcx,
80-
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
81-
codegen_fn_attrs: tcx.codegen_fn_attrs(body.source.def_id()),
89+
param_env,
90+
codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
8291
hir_id,
8392
history: Vec::new(),
8493
changed: false,

compiler/rustc_monomorphize/src/partitioning/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,16 @@ pub fn partition<'tcx>(
220220
let mut cgus: Vec<_> = post_inlining.codegen_units.iter_mut().collect();
221221
cgus.sort_by_key(|cgu| cgu.size_estimate());
222222

223-
let dead_code_cgu = if let Some(cgu) = cgus
224-
.into_iter()
225-
.rev()
226-
.filter(|cgu| cgu.items().iter().any(|(_, (linkage, _))| *linkage == Linkage::External))
227-
.next()
228-
{
229-
cgu
230-
} else {
231-
// If there are no CGUs that have externally linked items,
232-
// then we just pick the first CGU as a fallback.
233-
&mut post_inlining.codegen_units[0]
234-
};
223+
let dead_code_cgu =
224+
if let Some(cgu) = cgus.into_iter().rev().find(|cgu| {
225+
cgu.items().iter().any(|(_, (linkage, _))| *linkage == Linkage::External)
226+
}) {
227+
cgu
228+
} else {
229+
// If there are no CGUs that have externally linked items,
230+
// then we just pick the first CGU as a fallback.
231+
&mut post_inlining.codegen_units[0]
232+
};
235233
dead_code_cgu.make_code_coverage_dead_code_cgu();
236234
}
237235

compiler/rustc_parse/src/parser/diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ impl<'a> Parser<'a> {
21562156
| PatKind::TupleStruct(qself @ None, path, _)
21572157
| PatKind::Path(qself @ None, path) => match &first_pat.kind {
21582158
PatKind::Ident(_, ident, _) => {
2159-
path.segments.insert(0, PathSegment::from_ident(ident.clone()));
2159+
path.segments.insert(0, PathSegment::from_ident(*ident));
21602160
path.span = new_span;
21612161
show_sugg = true;
21622162
first_pat = pat;
@@ -2183,8 +2183,8 @@ impl<'a> Parser<'a> {
21832183
Path {
21842184
span: new_span,
21852185
segments: vec![
2186-
PathSegment::from_ident(old_ident.clone()),
2187-
PathSegment::from_ident(ident.clone()),
2186+
PathSegment::from_ident(*old_ident),
2187+
PathSegment::from_ident(*ident),
21882188
],
21892189
tokens: None,
21902190
},
@@ -2194,7 +2194,7 @@ impl<'a> Parser<'a> {
21942194
}
21952195
PatKind::Path(old_qself, old_path) => {
21962196
let mut segments = old_path.segments.clone();
2197-
segments.push(PathSegment::from_ident(ident.clone()));
2197+
segments.push(PathSegment::from_ident(*ident));
21982198
let path = PatKind::Path(
21992199
old_qself.clone(),
22002200
Path { span: new_span, segments, tokens: None },

0 commit comments

Comments
 (0)