Skip to content

Commit e41f378

Browse files
committed
Auto merge of #84112 - Dylan-DPC:rollup-tapsrzz, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #83669 (Issue 81508 fix) - #84014 (Improve trait/impl method discrepancy errors) - #84059 (Bump libc dependency of std to 0.2.93) - #84067 (clean up example on read_to_string) - #84079 (Improve test for `rustdoc::bare_urls` lint) - #84094 (Remove FixedSizeArray) - #84101 (rustdoc: Move crate loader to collect_intra_doc_links::early ) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3f8adde + 1ff117e commit e41f378

37 files changed

+671
-332
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1909,9 +1909,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
19091909

19101910
[[package]]
19111911
name = "libc"
1912-
version = "0.2.88"
1912+
version = "0.2.93"
19131913
source = "registry+https://github.com/rust-lang/crates.io-index"
1914-
checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a"
1914+
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
19151915
dependencies = [
19161916
"rustc-std-workspace-core",
19171917
]

compiler/rustc_middle/src/ty/error.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub enum TypeError<'tcx> {
3636
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
3737
AbiMismatch(ExpectedFound<abi::Abi>),
3838
Mutability,
39+
ArgumentMutability(usize),
3940
TupleSize(ExpectedFound<usize>),
4041
FixedArraySize(ExpectedFound<u64>),
4142
ArgCount,
@@ -46,6 +47,7 @@ pub enum TypeError<'tcx> {
4647
RegionsPlaceholderMismatch,
4748

4849
Sorts(ExpectedFound<Ty<'tcx>>),
50+
ArgumentSorts(ExpectedFound<Ty<'tcx>>, usize),
4951
IntMismatch(ExpectedFound<ty::IntVarValue>),
5052
FloatMismatch(ExpectedFound<ty::FloatTy>),
5153
Traits(ExpectedFound<DefId>),
@@ -110,7 +112,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
110112
AbiMismatch(values) => {
111113
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
112114
}
113-
Mutability => write!(f, "types differ in mutability"),
115+
ArgumentMutability(_) | Mutability => write!(f, "types differ in mutability"),
114116
TupleSize(values) => write!(
115117
f,
116118
"expected a tuple with {} element{}, \
@@ -142,7 +144,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
142144
br_string(br)
143145
),
144146
RegionsPlaceholderMismatch => write!(f, "one type is more general than the other"),
145-
Sorts(values) => ty::tls::with(|tcx| {
147+
ArgumentSorts(values, _) | Sorts(values) => ty::tls::with(|tcx| {
146148
report_maybe_different(
147149
f,
148150
&values.expected.sort_string(tcx),
@@ -199,10 +201,11 @@ impl<'tcx> TypeError<'tcx> {
199201
use self::TypeError::*;
200202
match self {
201203
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | Mismatch | AbiMismatch(_)
202-
| FixedArraySize(_) | Sorts(_) | IntMismatch(_) | FloatMismatch(_)
203-
| VariadicMismatch(_) | TargetFeatureCast(_) => false,
204+
| FixedArraySize(_) | ArgumentSorts(..) | Sorts(_) | IntMismatch(_)
205+
| FloatMismatch(_) | VariadicMismatch(_) | TargetFeatureCast(_) => false,
204206

205207
Mutability
208+
| ArgumentMutability(_)
206209
| TupleSize(_)
207210
| ArgCount
208211
| RegionsDoesNotOutlive(..)
@@ -339,7 +342,7 @@ impl<'tcx> TyCtxt<'tcx> {
339342
use self::TypeError::*;
340343
debug!("note_and_explain_type_err err={:?} cause={:?}", err, cause);
341344
match err {
342-
Sorts(values) => {
345+
ArgumentSorts(values, _) | Sorts(values) => {
343346
match (values.expected.kind(), values.found.kind()) {
344347
(ty::Closure(..), ty::Closure(..)) => {
345348
db.note("no two closures, even if identical, have the same type");

compiler/rustc_middle/src/ty/relate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
179179
} else {
180180
relation.relate_with_variance(ty::Contravariant, a, b)
181181
}
182+
})
183+
.enumerate()
184+
.map(|(i, r)| match r {
185+
Err(TypeError::Sorts(exp_found)) => Err(TypeError::ArgumentSorts(exp_found, i)),
186+
Err(TypeError::Mutability) => Err(TypeError::ArgumentMutability(i)),
187+
r => r,
182188
});
183189
Ok(ty::FnSig {
184190
inputs_and_output: tcx.mk_type_list(inputs_and_output)?,

compiler/rustc_middle/src/ty/structural_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
587587
UnsafetyMismatch(x) => UnsafetyMismatch(x),
588588
AbiMismatch(x) => AbiMismatch(x),
589589
Mutability => Mutability,
590+
ArgumentMutability(i) => ArgumentMutability(i),
590591
TupleSize(x) => TupleSize(x),
591592
FixedArraySize(x) => FixedArraySize(x),
592593
ArgCount => ArgCount,
@@ -607,6 +608,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
607608
CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
608609
CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
609610
ProjectionMismatched(x) => ProjectionMismatched(x),
611+
ArgumentSorts(x, i) => return tcx.lift(x).map(|x| ArgumentSorts(x, i)),
610612
Sorts(x) => return tcx.lift(x).map(Sorts),
611613
ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
612614
ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),

compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10311031
}
10321032

10331033
ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(_, ref ty, ref expr) => {
1034-
debug!("resolve_item ItemKind::Const");
10351034
self.with_item_rib(HasGenericParams::No, |this| {
10361035
this.visit_ty(ty);
10371036
if let Some(expr) = expr {
@@ -1597,6 +1596,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15971596
.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub)
15981597
.unwrap_or_else(|| self.fresh_binding(ident, pat.id, pat_src, bindings));
15991598
self.r.record_partial_res(pat.id, PartialRes::new(res));
1599+
self.r.record_pat_span(pat.id, pat.span);
16001600
}
16011601
PatKind::TupleStruct(ref path, ref sub_patterns) => {
16021602
self.smart_resolve_path(

compiler/rustc_resolve/src/lib.rs

+63-2
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ pub struct Resolver<'a> {
891891
/// "self-confirming" import resolutions during import validation.
892892
unusable_binding: Option<&'a NameBinding<'a>>,
893893

894+
// Spans for local variables found during pattern resolution.
895+
// Used for suggestions during error reporting.
896+
pat_span_map: NodeMap<Span>,
897+
894898
/// Resolutions for nodes that have a single resolution.
895899
partial_res_map: NodeMap<PartialRes>,
896900
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
@@ -1270,6 +1274,7 @@ impl<'a> Resolver<'a> {
12701274
last_import_segment: false,
12711275
unusable_binding: None,
12721276

1277+
pat_span_map: Default::default(),
12731278
partial_res_map: Default::default(),
12741279
import_res_map: Default::default(),
12751280
label_res_map: Default::default(),
@@ -1917,7 +1922,6 @@ impl<'a> Resolver<'a> {
19171922
return Some(LexicalScopeBinding::Item(binding));
19181923
}
19191924
}
1920-
19211925
self.early_resolve_ident_in_lexical_scope(
19221926
orig_ident,
19231927
ScopeSet::Late(ns, module, record_used_id),
@@ -2394,7 +2398,59 @@ impl<'a> Resolver<'a> {
23942398
.next()
23952399
.map_or(false, |c| c.is_ascii_uppercase())
23962400
{
2397-
(format!("use of undeclared type `{}`", ident), None)
2401+
// Check whether the name refers to an item in the value namespace.
2402+
let suggestion = if ribs.is_some() {
2403+
let match_span = match self.resolve_ident_in_lexical_scope(
2404+
ident,
2405+
ValueNS,
2406+
parent_scope,
2407+
None,
2408+
path_span,
2409+
&ribs.unwrap()[ValueNS],
2410+
) {
2411+
// Name matches a local variable. For example:
2412+
// ```
2413+
// fn f() {
2414+
// let Foo: &str = "";
2415+
// println!("{}", Foo::Bar); // Name refers to local
2416+
// // variable `Foo`.
2417+
// }
2418+
// ```
2419+
Some(LexicalScopeBinding::Res(Res::Local(id))) => {
2420+
Some(*self.pat_span_map.get(&id).unwrap())
2421+
}
2422+
2423+
// Name matches item from a local name binding
2424+
// created by `use` declaration. For example:
2425+
// ```
2426+
// pub Foo: &str = "";
2427+
//
2428+
// mod submod {
2429+
// use super::Foo;
2430+
// println!("{}", Foo::Bar); // Name refers to local
2431+
// // binding `Foo`.
2432+
// }
2433+
// ```
2434+
Some(LexicalScopeBinding::Item(name_binding)) => {
2435+
Some(name_binding.span)
2436+
}
2437+
_ => None,
2438+
};
2439+
2440+
if let Some(span) = match_span {
2441+
Some((
2442+
vec![(span, String::from(""))],
2443+
format!("`{}` is defined here, but is not a type", ident),
2444+
Applicability::MaybeIncorrect,
2445+
))
2446+
} else {
2447+
None
2448+
}
2449+
} else {
2450+
None
2451+
};
2452+
2453+
(format!("use of undeclared type `{}`", ident), suggestion)
23982454
} else {
23992455
(format!("use of undeclared crate or module `{}`", ident), None)
24002456
}
@@ -2805,6 +2861,11 @@ impl<'a> Resolver<'a> {
28052861
}
28062862
}
28072863

2864+
fn record_pat_span(&mut self, node: NodeId, span: Span) {
2865+
debug!("(recording pat) recording {:?} for {:?}", node, span);
2866+
self.pat_span_map.insert(node, span);
2867+
}
2868+
28082869
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
28092870
vis.is_accessible_from(module.nearest_parent_mod, self)
28102871
}

0 commit comments

Comments
 (0)