Skip to content

Commit a167cbd

Browse files
committed
Auto merge of #109164 - Dylan-DPC:rollup-0bwxwos, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #108991 (add `enable-warnings` flag for llvm, and disable it by default.) - #109109 (Use `unused_generic_params` from crate metadata) - #109111 (Create dirs for build_triple) - #109136 (Simplify proc macro signature validity check) - #109150 (Update cargo) - #109154 (Fix MappingToUnit to support no span of arg_ty) - #109157 (Remove mw from review rotation for a while) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents e4b9f86 + ff8f659 commit a167cbd

33 files changed

+295
-297
lines changed

Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ dependencies = [
353353

354354
[[package]]
355355
name = "cargo"
356-
version = "0.70.0"
356+
version = "0.71.0"
357357
dependencies = [
358358
"anyhow",
359359
"base64",
@@ -1001,7 +1001,7 @@ dependencies = [
10011001

10021002
[[package]]
10031003
name = "crates-io"
1004-
version = "0.35.1"
1004+
version = "0.36.0"
10051005
dependencies = [
10061006
"anyhow",
10071007
"curl",
@@ -5139,6 +5139,7 @@ dependencies = [
51395139
"rustc_session",
51405140
"rustc_span",
51415141
"rustc_target",
5142+
"rustc_trait_selection",
51425143
"tracing",
51435144
]
51445145

compiler/rustc_lint/src/map_unit_fn.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
5656
return;
5757
}
5858
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
59+
let default_span = args[0].span;
5960
if let ty::FnDef(id, _) = arg_ty.kind() {
6061
let fn_ty = cx.tcx.fn_sig(id).skip_binder();
6162
let ret_ty = fn_ty.output().skip_binder();
@@ -64,7 +65,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
6465
MAP_UNIT_FN,
6566
span,
6667
MappingToUnit {
67-
function_label: cx.tcx.span_of_impl(*id).unwrap(),
68+
function_label: cx
69+
.tcx
70+
.span_of_impl(*id)
71+
.unwrap_or(default_span),
6872
argument_label: args[0].span,
6973
map_label: arg_ty.default_span(cx.tcx),
7074
suggestion: path.ident.span,
@@ -80,7 +84,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
8084
MAP_UNIT_FN,
8185
span,
8286
MappingToUnit {
83-
function_label: cx.tcx.span_of_impl(*id).unwrap(),
87+
function_label: cx
88+
.tcx
89+
.span_of_impl(*id)
90+
.unwrap_or(default_span),
8491
argument_label: args[0].span,
8592
map_label: arg_ty.default_span(cx.tcx),
8693
suggestion: path.ident.span,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,15 @@ provide! { tcx, def_id, other, cdata,
226226
lookup_default_body_stability => { table }
227227
lookup_deprecation_entry => { table }
228228
params_in_repr => { table }
229-
unused_generic_params => { table }
229+
// FIXME: Could be defaulted, but `LazyValue<UnusedGenericParams>` is not `FixedSizeEncoding`..
230+
unused_generic_params => {
231+
cdata
232+
.root
233+
.tables
234+
.unused_generic_params
235+
.get(cdata, def_id.index)
236+
.map_or_else(|| ty::UnusedGenericParams::new_all_used(), |lazy| lazy.decode((cdata, tcx)))
237+
}
230238
opt_def_kind => { table_direct }
231239
impl_parent => { table }
232240
impl_polarity => { table_direct }

compiler/rustc_middle/src/query/keys.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx> Key for ty::InstanceDef<'tcx> {
6363

6464
#[inline(always)]
6565
fn query_crate_is_local(&self) -> bool {
66-
true
66+
self.def_id().is_local()
6767
}
6868

6969
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -76,7 +76,7 @@ impl<'tcx> Key for ty::Instance<'tcx> {
7676

7777
#[inline(always)]
7878
fn query_crate_is_local(&self) -> bool {
79-
true
79+
self.def_id().is_local()
8080
}
8181

8282
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {

compiler/rustc_monomorphize/src/polymorphize.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ fn unused_generic_params<'tcx>(
3636
tcx: TyCtxt<'tcx>,
3737
instance: ty::InstanceDef<'tcx>,
3838
) -> UnusedGenericParams {
39+
assert!(instance.def_id().is_local());
40+
3941
if !tcx.sess.opts.unstable_opts.polymorphize {
4042
// If polymorphization disabled, then all parameters are used.
4143
return UnusedGenericParams::new_all_used();
@@ -100,13 +102,6 @@ fn should_polymorphize<'tcx>(
100102
return false;
101103
}
102104

103-
// Polymorphization results are stored in cross-crate metadata only when there are unused
104-
// parameters, so assume that non-local items must have only used parameters (else this query
105-
// would not be invoked, and the cross-crate metadata used instead).
106-
if !def_id.is_local() {
107-
return false;
108-
}
109-
110105
// Foreign items have no bodies to analyze.
111106
if tcx.is_foreign_item(def_id) {
112107
return false;

compiler/rustc_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ rustc_span = { path = "../rustc_span" }
2222
rustc_lexer = { path = "../rustc_lexer" }
2323
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
2424
rustc_feature = { path = "../rustc_feature" }
25+
rustc_trait_selection = { path = "../rustc_trait_selection" }

compiler/rustc_passes/messages.ftl

+1-20
Original file line numberDiff line numberDiff line change
@@ -720,26 +720,7 @@ passes_ignored_derived_impls =
720720
*[other] traits {$trait_list}, but these are
721721
} intentionally ignored during dead code analysis
722722
723-
passes_proc_macro_typeerror = mismatched {$kind} signature
724-
.label = found {$found}, expected type `proc_macro::TokenStream`
725-
.note = {$kind}s must have a signature of `{$expected_signature}`
726-
727-
passes_proc_macro_diff_arg_count = mismatched {$kind} signature
728-
.label = found unexpected {$count ->
729-
[one] argument
730-
*[other] arguments
731-
}
732-
.note = {$kind}s must have a signature of `{$expected_signature}`
733-
734-
passes_proc_macro_missing_args = mismatched {$kind} signature
735-
.label = {$kind} must have {$expected_input_count ->
736-
[one] one argument
737-
*[other] two arguments
738-
} of type `proc_macro::TokenStream`
739-
740-
passes_proc_macro_invalid_abi = proc macro functions may not be `extern "{$abi}"`
741-
742-
passes_proc_macro_unsafe = proc macro functions may not be `unsafe`
723+
passes_proc_macro_bad_sig = {$kind} has incorrect signature
743724
744725
passes_skipping_const_checks = skipping const checks
745726

compiler/rustc_passes/src/check_attr.rs

+88-85
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ use rustc_hir::{
1919
use rustc_hir::{MethodKind, Target, Unsafety};
2020
use rustc_middle::hir::nested_filter;
2121
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
22-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
22+
use rustc_middle::traits::ObligationCause;
23+
use rustc_middle::ty::error::{ExpectedFound, TypeError};
2324
use rustc_middle::ty::query::Providers;
24-
use rustc_middle::ty::{ParamEnv, TyCtxt};
25+
use rustc_middle::ty::{self, TyCtxt};
2526
use rustc_session::lint::builtin::{
2627
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
2728
UNUSED_ATTRIBUTES,
@@ -30,6 +31,9 @@ use rustc_session::parse::feature_err;
3031
use rustc_span::symbol::{kw, sym, Symbol};
3132
use rustc_span::{Span, DUMMY_SP};
3233
use rustc_target::spec::abi::Abi;
34+
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
35+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
36+
use rustc_trait_selection::traits::ObligationCtxt;
3337
use std::cell::Cell;
3438
use std::collections::hash_map::Entry;
3539

@@ -2188,100 +2192,99 @@ impl CheckAttrVisitor<'_> {
21882192
///
21892193
/// If this best effort goes wrong, it will just emit a worse error later (see #102923)
21902194
fn check_proc_macro(&self, hir_id: HirId, target: Target, kind: ProcMacroKind) {
2191-
let expected_input_count = match kind {
2192-
ProcMacroKind::Attribute => 2,
2193-
ProcMacroKind::Derive | ProcMacroKind::FunctionLike => 1,
2194-
};
2195-
2196-
let expected_signature = match kind {
2197-
ProcMacroKind::Attribute => "fn(TokenStream, TokenStream) -> TokenStream",
2198-
ProcMacroKind::Derive | ProcMacroKind::FunctionLike => "fn(TokenStream) -> TokenStream",
2199-
};
2195+
if target != Target::Fn {
2196+
return;
2197+
}
22002198

22012199
let tcx = self.tcx;
2202-
if target == Target::Fn {
2203-
let Some(tokenstream) = tcx.get_diagnostic_item(sym::TokenStream) else {return};
2204-
let tokenstream = tcx.type_of(tokenstream).subst_identity();
2205-
2206-
let id = hir_id.expect_owner();
2207-
let hir_sig = tcx.hir().fn_sig_by_hir_id(hir_id).unwrap();
2208-
2209-
let sig =
2210-
tcx.liberate_late_bound_regions(id.to_def_id(), tcx.fn_sig(id).subst_identity());
2211-
let sig = tcx.normalize_erasing_regions(ParamEnv::empty(), sig);
2212-
2213-
// We don't currently require that the function signature is equal to
2214-
// `fn(TokenStream) -> TokenStream`, but instead monomorphizes to
2215-
// `fn(TokenStream) -> TokenStream` after some substitution of generic arguments.
2216-
//
2217-
// Properly checking this means pulling in additional `rustc` crates, so we don't.
2218-
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsCandidateKey };
2219-
2220-
if sig.abi != Abi::Rust {
2221-
tcx.sess.emit_err(errors::ProcMacroInvalidAbi {
2222-
span: hir_sig.span,
2223-
abi: sig.abi.name(),
2224-
});
2225-
self.abort.set(true);
2226-
}
2200+
let Some(token_stream_def_id) = tcx.get_diagnostic_item(sym::TokenStream) else { return; };
2201+
let Some(token_stream) = tcx.type_of(token_stream_def_id).no_bound_vars() else { return; };
22272202

2228-
if sig.unsafety == Unsafety::Unsafe {
2229-
tcx.sess.emit_err(errors::ProcMacroUnsafe { span: hir_sig.span });
2230-
self.abort.set(true);
2231-
}
2203+
let def_id = hir_id.expect_owner().def_id;
2204+
let param_env = ty::ParamEnv::empty();
22322205

2233-
let output = sig.output();
2206+
let infcx = tcx.infer_ctxt().build();
2207+
let ocx = ObligationCtxt::new(&infcx);
22342208

2235-
// Typecheck the output
2236-
if !drcx.types_may_unify(output, tokenstream) {
2237-
tcx.sess.emit_err(errors::ProcMacroTypeError {
2238-
span: hir_sig.decl.output.span(),
2239-
found: output,
2240-
kind,
2241-
expected_signature,
2242-
});
2243-
self.abort.set(true);
2244-
}
2209+
let span = tcx.def_span(def_id);
2210+
let fresh_substs = infcx.fresh_substs_for_item(span, def_id.to_def_id());
2211+
let sig = tcx.liberate_late_bound_regions(
2212+
def_id.to_def_id(),
2213+
tcx.fn_sig(def_id).subst(tcx, fresh_substs),
2214+
);
22452215

2246-
if sig.inputs().len() < expected_input_count {
2247-
tcx.sess.emit_err(errors::ProcMacroMissingArguments {
2248-
expected_input_count,
2249-
span: hir_sig.span,
2250-
kind,
2251-
expected_signature,
2252-
});
2253-
self.abort.set(true);
2254-
}
2216+
let mut cause = ObligationCause::misc(span, def_id);
2217+
let sig = ocx.normalize(&cause, param_env, sig);
22552218

2256-
// Check that the inputs are correct, if there are enough.
2257-
if sig.inputs().len() >= expected_input_count {
2258-
for (arg, input) in
2259-
sig.inputs().iter().zip(hir_sig.decl.inputs).take(expected_input_count)
2260-
{
2261-
if !drcx.types_may_unify(*arg, tokenstream) {
2262-
tcx.sess.emit_err(errors::ProcMacroTypeError {
2263-
span: input.span,
2264-
found: *arg,
2265-
kind,
2266-
expected_signature,
2267-
});
2268-
self.abort.set(true);
2219+
// proc macro is not WF.
2220+
let errors = ocx.select_where_possible();
2221+
if !errors.is_empty() {
2222+
return;
2223+
}
2224+
2225+
let expected_sig = tcx.mk_fn_sig(
2226+
std::iter::repeat(token_stream).take(match kind {
2227+
ProcMacroKind::Attribute => 2,
2228+
ProcMacroKind::Derive | ProcMacroKind::FunctionLike => 1,
2229+
}),
2230+
token_stream,
2231+
false,
2232+
Unsafety::Normal,
2233+
Abi::Rust,
2234+
);
2235+
2236+
if let Err(terr) = ocx.eq(&cause, param_env, expected_sig, sig) {
2237+
let mut diag = tcx.sess.create_err(errors::ProcMacroBadSig { span, kind });
2238+
2239+
let hir_sig = tcx.hir().fn_sig_by_hir_id(hir_id);
2240+
if let Some(hir_sig) = hir_sig {
2241+
match terr {
2242+
TypeError::ArgumentMutability(idx) | TypeError::ArgumentSorts(_, idx) => {
2243+
if let Some(ty) = hir_sig.decl.inputs.get(idx) {
2244+
diag.set_span(ty.span);
2245+
cause.span = ty.span;
2246+
} else if idx == hir_sig.decl.inputs.len() {
2247+
let span = hir_sig.decl.output.span();
2248+
diag.set_span(span);
2249+
cause.span = span;
2250+
}
2251+
}
2252+
TypeError::ArgCount => {
2253+
if let Some(ty) = hir_sig.decl.inputs.get(expected_sig.inputs().len()) {
2254+
diag.set_span(ty.span);
2255+
cause.span = ty.span;
2256+
}
22692257
}
2258+
TypeError::UnsafetyMismatch(_) => {
2259+
// FIXME: Would be nice if we had a span here..
2260+
}
2261+
TypeError::AbiMismatch(_) => {
2262+
// FIXME: Would be nice if we had a span here..
2263+
}
2264+
TypeError::VariadicMismatch(_) => {
2265+
// FIXME: Would be nice if we had a span here..
2266+
}
2267+
_ => {}
22702268
}
22712269
}
22722270

2273-
// Check that there are not too many arguments
2274-
let body_id = tcx.hir().body_owned_by(id.def_id);
2275-
let excess = tcx.hir().body(body_id).params.get(expected_input_count..);
2276-
if let Some(excess @ [begin @ end] | excess @ [begin, .., end]) = excess {
2277-
tcx.sess.emit_err(errors::ProcMacroDiffArguments {
2278-
span: begin.span.to(end.span),
2279-
count: excess.len(),
2280-
kind,
2281-
expected_signature,
2282-
});
2283-
self.abort.set(true);
2284-
}
2271+
infcx.err_ctxt().note_type_err(
2272+
&mut diag,
2273+
&cause,
2274+
None,
2275+
Some(ValuePairs::Sigs(ExpectedFound { expected: expected_sig, found: sig })),
2276+
terr,
2277+
false,
2278+
false,
2279+
);
2280+
diag.emit();
2281+
self.abort.set(true);
2282+
}
2283+
2284+
let errors = ocx.select_all_or_error();
2285+
if !errors.is_empty() {
2286+
infcx.err_ctxt().report_fulfillment_errors(&errors);
2287+
self.abort.set(true);
22852288
}
22862289
}
22872290
}

0 commit comments

Comments
 (0)