From 7b9ad6b4b7306af757908833c2f5e54102a3a2ff Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 9 Sep 2020 03:19:01 -0400 Subject: Document speculative crate loading (#859) --- src/doc/rustc-dev-guide/src/name-resolution.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/doc/rustc-dev-guide') diff --git a/src/doc/rustc-dev-guide/src/name-resolution.md b/src/doc/rustc-dev-guide/src/name-resolution.md index ebd44da1d76..3785e3aa782 100644 --- a/src/doc/rustc-dev-guide/src/name-resolution.md +++ b/src/doc/rustc-dev-guide/src/name-resolution.md @@ -145,6 +145,23 @@ the code can be processed. Therefore, the resolution is performed in multiple stages. +## Speculative crate loading + +To give useful errors, rustc suggests importing paths into scope if they're +not found. How does it do this? It looks through every module of every crate +and looks for possible matches. This even includes crates that haven't yet +been loaded! + +Loading crates for import suggestions that haven't yet been loaded is called +_speculative crate loading_, because any errors it encounters shouldn't be +reported: resolve decided to load them, not the user. The function that does +this is `lookup_import_candidates` and lives in +`rustc_resolve/src/diagnostics.rs`. + +To tell the difference between speculative loads and loads initiated by the +user, resolve passes around a `record_used` parameter, which is `false` when +the load is speculative. + ## TODO: This is a result of the first pass of learning the code. It is definitely -- cgit 1.4.1-3-g733a5