diff --git a/src/tools/rust-analyzer/.git-blame-ignore-revs b/src/tools/rust-analyzer/.git-blame-ignore-revs index a302e23781ae9..d5951a9420916 100644 --- a/src/tools/rust-analyzer/.git-blame-ignore-revs +++ b/src/tools/rust-analyzer/.git-blame-ignore-revs @@ -6,3 +6,10 @@ # prettier format f247090558c9ba3c551566eae5882b7ca865225f + +# subtree syncs +932d85b52946d917deab2c23ead552f7f713b828 +3e358a6827d83e8d6473913a5e304734aadfed04 +9d2cb42a413e51deb50b36794a2e1605381878fc +f532576ac53ddcc666bc8d59e0b6437065e2f599 +c48062fe2ab9a2d913d1985a6b0aec4bf936bfc1 diff --git a/src/tools/rust-analyzer/Cargo.lock b/src/tools/rust-analyzer/Cargo.lock index a6e460134f21c..d3400eefb0aad 100644 --- a/src/tools/rust-analyzer/Cargo.lock +++ b/src/tools/rust-analyzer/Cargo.lock @@ -323,6 +323,27 @@ dependencies = [ "syn", ] +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dissimilar" version = "1.0.7" @@ -898,6 +919,16 @@ dependencies = [ "libc", ] +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.4.2", + "libc", +] + [[package]] name = "limit" version = "0.0.0" @@ -1186,6 +1217,12 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "parking_lot" version = "0.12.1" @@ -1566,6 +1603,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "rowan" version = "0.15.15" @@ -2255,6 +2303,7 @@ dependencies = [ "paths", "rustc-hash", "stdx", + "tracing", ] [[package]] @@ -2499,6 +2548,7 @@ name = "xtask" version = "0.1.0" dependencies = [ "anyhow", + "directories", "flate2", "itertools", "proc-macro2", diff --git a/src/tools/rust-analyzer/crates/base-db/src/lib.rs b/src/tools/rust-analyzer/crates/base-db/src/lib.rs index 2b64a07a5a95f..82aff6e3b8e35 100644 --- a/src/tools/rust-analyzer/crates/base-db/src/lib.rs +++ b/src/tools/rust-analyzer/crates/base-db/src/lib.rs @@ -51,6 +51,7 @@ pub trait FileLoader { /// Text of the file. fn file_text(&self, file_id: FileId) -> Arc; fn resolve_path(&self, path: AnchoredPath<'_>) -> Option; + /// Crates whose root's source root is the same as the source root of `file_id` fn relevant_crates(&self, file_id: FileId) -> Arc<[CrateId]>; } @@ -104,6 +105,7 @@ pub trait SourceDatabaseExt: SourceDatabase { #[salsa::input] fn source_root(&self, id: SourceRootId) -> Arc; + /// Crates whose root fool is in `id`. fn source_root_crates(&self, id: SourceRootId) -> Arc<[CrateId]>; } diff --git a/src/tools/rust-analyzer/crates/flycheck/src/lib.rs b/src/tools/rust-analyzer/crates/flycheck/src/lib.rs index 5dfaaf774209c..6d5ca8321e5dc 100644 --- a/src/tools/rust-analyzer/crates/flycheck/src/lib.rs +++ b/src/tools/rust-analyzer/crates/flycheck/src/lib.rs @@ -125,8 +125,10 @@ impl FlycheckHandle { config: FlycheckConfig, sysroot_root: Option, workspace_root: AbsPathBuf, + manifest_path: Option, ) -> FlycheckHandle { - let actor = FlycheckActor::new(id, sender, config, sysroot_root, workspace_root); + let actor = + FlycheckActor::new(id, sender, config, sysroot_root, workspace_root, manifest_path); let (sender, receiver) = unbounded::(); let thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker) .name("Flycheck".to_owned()) @@ -205,6 +207,7 @@ struct FlycheckActor { id: usize, sender: Box, config: FlycheckConfig, + manifest_path: Option, /// Either the workspace root of the workspace we are flychecking, /// or the project root of the project. root: AbsPathBuf, @@ -233,6 +236,7 @@ impl FlycheckActor { config: FlycheckConfig, sysroot_root: Option, workspace_root: AbsPathBuf, + manifest_path: Option, ) -> FlycheckActor { tracing::info!(%id, ?workspace_root, "Spawning flycheck"); FlycheckActor { @@ -241,6 +245,7 @@ impl FlycheckActor { config, sysroot_root, root: workspace_root, + manifest_path, command_handle: None, command_receiver: None, } @@ -388,8 +393,13 @@ impl FlycheckActor { "--message-format=json" }); - cmd.arg("--manifest-path"); - cmd.arg(self.root.join("Cargo.toml")); + if let Some(manifest_path) = &self.manifest_path { + cmd.arg("--manifest-path"); + cmd.arg(manifest_path); + if manifest_path.extension().map_or(false, |ext| ext == "rs") { + cmd.arg("-Zscript"); + } + } options.apply_on_command(&mut cmd); (cmd, options.extra_args.clone()) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs b/src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs index 340e95dbc2f43..f58228c45f0b8 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs @@ -1869,42 +1869,45 @@ impl ExprCollector<'_> { ) -> ExprId { match count { Some(FormatCount::Literal(n)) => { - match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Is]) { - Some(count_is) => { - let count_is = self.alloc_expr_desugared(Expr::Path(count_is)); - let args = self.alloc_expr_desugared(Expr::Literal(Literal::Uint( - *n as u128, - Some(BuiltinUint::Usize), - ))); - self.alloc_expr_desugared(Expr::Call { - callee: count_is, - args: Box::new([args]), - is_assignee_expr: false, - }) - } - None => self.missing_expr(), - } + let args = self.alloc_expr_desugared(Expr::Literal(Literal::Uint( + *n as u128, + Some(BuiltinUint::Usize), + ))); + let count_is = + match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Is]) { + Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)), + None => self.missing_expr(), + }; + self.alloc_expr_desugared(Expr::Call { + callee: count_is, + args: Box::new([args]), + is_assignee_expr: false, + }) } Some(FormatCount::Argument(arg)) => { if let Ok(arg_index) = arg.index { let (i, _) = argmap.insert_full((arg_index, ArgumentType::Usize)); - match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Param]) { - Some(count_param) => { - let count_param = self.alloc_expr_desugared(Expr::Path(count_param)); - let args = self.alloc_expr_desugared(Expr::Literal(Literal::Uint( - i as u128, - Some(BuiltinUint::Usize), - ))); - self.alloc_expr_desugared(Expr::Call { - callee: count_param, - args: Box::new([args]), - is_assignee_expr: false, - }) - } + let args = self.alloc_expr_desugared(Expr::Literal(Literal::Uint( + i as u128, + Some(BuiltinUint::Usize), + ))); + let count_param = match LangItem::FormatCount.ty_rel_path( + self.db, + self.krate, + name![Param], + ) { + Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)), None => self.missing_expr(), - } + }; + self.alloc_expr_desugared(Expr::Call { + callee: count_param, + args: Box::new([args]), + is_assignee_expr: false, + }) } else { + // FIXME: This drops arg causing it to potentially not be resolved/type checked + // when typing? self.missing_expr() } } @@ -1925,7 +1928,8 @@ impl ExprCollector<'_> { fn make_argument(&mut self, arg: ExprId, ty: ArgumentType) -> ExprId { use ArgumentType::*; use FormatTrait::*; - match LangItem::FormatArgument.ty_rel_path( + + let new_fn = match LangItem::FormatArgument.ty_rel_path( self.db, self.krate, match ty { @@ -1941,16 +1945,14 @@ impl ExprCollector<'_> { Usize => name![from_usize], }, ) { - Some(new_fn) => { - let new_fn = self.alloc_expr_desugared(Expr::Path(new_fn)); - self.alloc_expr_desugared(Expr::Call { - callee: new_fn, - args: Box::new([arg]), - is_assignee_expr: false, - }) - } + Some(new_fn) => self.alloc_expr_desugared(Expr::Path(new_fn)), None => self.missing_expr(), - } + }; + self.alloc_expr_desugared(Expr::Call { + callee: new_fn, + args: Box::new([arg]), + is_assignee_expr: false, + }) } // endregion: format } diff --git a/src/tools/rust-analyzer/crates/hir-def/src/body/scope.rs b/src/tools/rust-analyzer/crates/hir-def/src/body/scope.rs index 0020e4eac3072..fd685235e17db 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/body/scope.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/body/scope.rs @@ -7,7 +7,7 @@ use crate::{ body::Body, db::DefDatabase, hir::{Binding, BindingId, Expr, ExprId, LabelId, Pat, PatId, Statement}, - BlockId, DefWithBodyId, + BlockId, ConstBlockId, DefWithBodyId, }; pub type ScopeId = Idx; @@ -46,7 +46,9 @@ pub struct ScopeData { impl ExprScopes { pub(crate) fn expr_scopes_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc { let body = db.body(def); - let mut scopes = ExprScopes::new(&body); + let mut scopes = ExprScopes::new(&body, |const_block| { + db.lookup_intern_anonymous_const(const_block).root + }); scopes.shrink_to_fit(); Arc::new(scopes) } @@ -89,7 +91,10 @@ fn empty_entries(idx: usize) -> IdxRange { } impl ExprScopes { - fn new(body: &Body) -> ExprScopes { + fn new( + body: &Body, + resolve_const_block: impl (Fn(ConstBlockId) -> ExprId) + Copy, + ) -> ExprScopes { let mut scopes = ExprScopes { scopes: Arena::default(), scope_entries: Arena::default(), @@ -100,7 +105,7 @@ impl ExprScopes { scopes.add_bindings(body, root, self_param); } scopes.add_params_bindings(body, root, &body.params); - compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root); + compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root, resolve_const_block); scopes } @@ -183,35 +188,46 @@ fn compute_block_scopes( body: &Body, scopes: &mut ExprScopes, scope: &mut ScopeId, + resolve_const_block: impl (Fn(ConstBlockId) -> ExprId) + Copy, ) { for stmt in statements { match stmt { Statement::Let { pat, initializer, else_branch, .. } => { if let Some(expr) = initializer { - compute_expr_scopes(*expr, body, scopes, scope); + compute_expr_scopes(*expr, body, scopes, scope, resolve_const_block); } if let Some(expr) = else_branch { - compute_expr_scopes(*expr, body, scopes, scope); + compute_expr_scopes(*expr, body, scopes, scope, resolve_const_block); } *scope = scopes.new_scope(*scope); scopes.add_pat_bindings(body, *scope, *pat); } Statement::Expr { expr, .. } => { - compute_expr_scopes(*expr, body, scopes, scope); + compute_expr_scopes(*expr, body, scopes, scope, resolve_const_block); } Statement::Item => (), } } if let Some(expr) = tail { - compute_expr_scopes(expr, body, scopes, scope); + compute_expr_scopes(expr, body, scopes, scope, resolve_const_block); } } -fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: &mut ScopeId) { +fn compute_expr_scopes( + expr: ExprId, + body: &Body, + scopes: &mut ExprScopes, + scope: &mut ScopeId, + resolve_const_block: impl (Fn(ConstBlockId) -> ExprId) + Copy, +) { let make_label = |label: &Option| label.map(|label| (label, body.labels[label].name.clone())); + let compute_expr_scopes = |scopes: &mut ExprScopes, expr: ExprId, scope: &mut ScopeId| { + compute_expr_scopes(expr, body, scopes, scope, resolve_const_block) + }; + scopes.set_scope(expr, *scope); match &body[expr] { Expr::Block { statements, tail, id, label } => { @@ -219,53 +235,54 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope // Overwrite the old scope for the block expr, so that every block scope can be found // via the block itself (important for blocks that only contain items, no expressions). scopes.set_scope(expr, scope); - compute_block_scopes(statements, *tail, body, scopes, &mut scope); + compute_block_scopes(statements, *tail, body, scopes, &mut scope, resolve_const_block); } - Expr::Const(_) => { - // FIXME: This is broken. + Expr::Const(id) => { + let mut scope = scopes.root_scope(); + compute_expr_scopes(scopes, resolve_const_block(*id), &mut scope); } Expr::Unsafe { id, statements, tail } | Expr::Async { id, statements, tail } => { let mut scope = scopes.new_block_scope(*scope, *id, None); // Overwrite the old scope for the block expr, so that every block scope can be found // via the block itself (important for blocks that only contain items, no expressions). scopes.set_scope(expr, scope); - compute_block_scopes(statements, *tail, body, scopes, &mut scope); + compute_block_scopes(statements, *tail, body, scopes, &mut scope, resolve_const_block); } Expr::Loop { body: body_expr, label } => { let mut scope = scopes.new_labeled_scope(*scope, make_label(label)); - compute_expr_scopes(*body_expr, body, scopes, &mut scope); + compute_expr_scopes(scopes, *body_expr, &mut scope); } Expr::Closure { args, body: body_expr, .. } => { let mut scope = scopes.new_scope(*scope); scopes.add_params_bindings(body, scope, args); - compute_expr_scopes(*body_expr, body, scopes, &mut scope); + compute_expr_scopes(scopes, *body_expr, &mut scope); } Expr::Match { expr, arms } => { - compute_expr_scopes(*expr, body, scopes, scope); + compute_expr_scopes(scopes, *expr, scope); for arm in arms.iter() { let mut scope = scopes.new_scope(*scope); scopes.add_pat_bindings(body, scope, arm.pat); if let Some(guard) = arm.guard { scope = scopes.new_scope(scope); - compute_expr_scopes(guard, body, scopes, &mut scope); + compute_expr_scopes(scopes, guard, &mut scope); } - compute_expr_scopes(arm.expr, body, scopes, &mut scope); + compute_expr_scopes(scopes, arm.expr, &mut scope); } } &Expr::If { condition, then_branch, else_branch } => { let mut then_branch_scope = scopes.new_scope(*scope); - compute_expr_scopes(condition, body, scopes, &mut then_branch_scope); - compute_expr_scopes(then_branch, body, scopes, &mut then_branch_scope); + compute_expr_scopes(scopes, condition, &mut then_branch_scope); + compute_expr_scopes(scopes, then_branch, &mut then_branch_scope); if let Some(else_branch) = else_branch { - compute_expr_scopes(else_branch, body, scopes, scope); + compute_expr_scopes(scopes, else_branch, scope); } } &Expr::Let { pat, expr } => { - compute_expr_scopes(expr, body, scopes, scope); + compute_expr_scopes(scopes, expr, scope); *scope = scopes.new_scope(*scope); scopes.add_pat_bindings(body, *scope, pat); } - e => e.walk_child_exprs(|e| compute_expr_scopes(e, body, scopes, scope)), + e => e.walk_child_exprs(|e| compute_expr_scopes(scopes, e, scope)), }; } diff --git a/src/tools/rust-analyzer/crates/hir-def/src/body/tests.rs b/src/tools/rust-analyzer/crates/hir-def/src/body/tests.rs index a27ffe216750e..4c8a54f7c8c72 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/body/tests.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/body/tests.rs @@ -318,18 +318,20 @@ fn f() { expect![[r#" fn f() { - $crate::panicking::panic_fmt( - builtin#lang(Arguments::new_v1_formatted)( - &[ - "cc", - ], - &[], - &[], - unsafe { - builtin#lang(UnsafeArg::new)() - }, - ), - ); + { + $crate::panicking::panic_fmt( + builtin#lang(Arguments::new_v1_formatted)( + &[ + "cc", + ], + &[], + &[], + unsafe { + builtin#lang(UnsafeArg::new)() + }, + ), + ); + }; }"#]] .assert_eq(&body.pretty_print(&db, def)) } diff --git a/src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs b/src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs index 610480736ccda..4bf4bc768f063 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs @@ -68,10 +68,10 @@ use crate::{ pub struct RawVisibilityId(u32); impl RawVisibilityId { - pub const PUB: Self = RawVisibilityId(u32::max_value()); - pub const PRIV_IMPLICIT: Self = RawVisibilityId(u32::max_value() - 1); - pub const PRIV_EXPLICIT: Self = RawVisibilityId(u32::max_value() - 2); - pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 3); + pub const PUB: Self = RawVisibilityId(u32::MAX); + pub const PRIV_IMPLICIT: Self = RawVisibilityId(u32::MAX - 1); + pub const PRIV_EXPLICIT: Self = RawVisibilityId(u32::MAX - 2); + pub const PUB_CRATE: Self = RawVisibilityId(u32::MAX - 3); } impl fmt::Debug for RawVisibilityId { diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs b/src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs index 0bf01b0bc6ae2..d99ef6679e51f 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs @@ -27,6 +27,7 @@ pub trait TyExt { fn is_scalar(&self) -> bool; fn is_floating_point(&self) -> bool; fn is_never(&self) -> bool; + fn is_str(&self) -> bool; fn is_unknown(&self) -> bool; fn contains_unknown(&self) -> bool; fn is_ty_var(&self) -> bool; @@ -87,6 +88,10 @@ impl TyExt for Ty { matches!(self.kind(Interner), TyKind::Never) } + fn is_str(&self) -> bool { + matches!(self.kind(Interner), TyKind::Str) + } + fn is_unknown(&self) -> bool { matches!(self.kind(Interner), TyKind::Error) } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs index a357e85035159..c010f5d22b681 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs @@ -797,8 +797,20 @@ impl HirDisplay for Ty { c.hir_fmt(f)?; write!(f, "]")?; } - TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => { - if matches!(self.kind(Interner), TyKind::Raw(..)) { + kind @ (TyKind::Raw(m, t) | TyKind::Ref(m, _, t)) => { + if let TyKind::Ref(_, l, _) = kind { + f.write_char('&')?; + if cfg!(test) { + // rendering these unconditionally is probably too much (at least for inlay + // hints) so we gate it to testing only for the time being + l.hir_fmt(f)?; + f.write_char(' ')?; + } + match m { + Mutability::Not => (), + Mutability::Mut => f.write_str("mut ")?, + } + } else { write!( f, "*{}", @@ -807,15 +819,6 @@ impl HirDisplay for Ty { Mutability::Mut => "mut ", } )?; - } else { - write!( - f, - "&{}", - match m { - Mutability::Not => "", - Mutability::Mut => "mut ", - } - )?; } // FIXME: all this just to decide whether to use parentheses... @@ -1330,7 +1333,18 @@ fn hir_fmt_generics( } let parameters_to_write = generic_args_sans_defaults(f, generic_def, parameters); - if !parameters_to_write.is_empty() { + + // FIXME: Remote this + // most of our lifetimes will be errors as we lack elision and inference + // so don't render them for now + let only_err_lifetimes = !cfg!(test) + && parameters_to_write.iter().all(|arg| { + matches!( + arg.data(Interner), + chalk_ir::GenericArgData::Lifetime(it) if *it.data(Interner) == LifetimeData::Error + ) + }); + if !parameters_to_write.is_empty() && !only_err_lifetimes { write!(f, "<")?; hir_fmt_generic_arguments(f, parameters_to_write)?; write!(f, ">")?; @@ -1403,6 +1417,18 @@ fn hir_fmt_generic_arguments( None => (parameters, &[][..]), }; for generic_arg in lifetimes.iter().chain(ty_or_const) { + // FIXME: Remove this + // most of our lifetimes will be errors as we lack elision and inference + // so don't render them for now + if !cfg!(test) + && matches!( + generic_arg.lifetime(Interner), + Some(l) if ***l.interned() == LifetimeData::Error + ) + { + continue; + } + if !first { write!(f, ", ")?; } @@ -1728,9 +1754,9 @@ impl HirDisplay for LifetimeData { LifetimeData::BoundVar(idx) => idx.hir_fmt(f), LifetimeData::InferenceVar(_) => write!(f, "_"), LifetimeData::Static => write!(f, "'static"), - LifetimeData::Error => write!(f, "'{{error}}"), - LifetimeData::Erased => Ok(()), - LifetimeData::Phantom(_, _) => Ok(()), + LifetimeData::Error => write!(f, "'?"), + LifetimeData::Erased => write!(f, "'"), + LifetimeData::Phantom(void, _) => match *void {}, } } } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs index 281386e1364b4..6f2f70dd40a78 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs @@ -198,6 +198,7 @@ pub enum InferenceDiagnostic { NoSuchField { field: ExprOrPatId, private: bool, + variant: VariantId, }, PrivateField { expr: ExprId, diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs index d011a62e77a34..38076fce8f8f5 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs @@ -563,6 +563,7 @@ impl InferenceContext<'_> { InferenceDiagnostic::NoSuchField { field: field.expr.into(), private: true, + variant: def, }, ); } @@ -572,6 +573,7 @@ impl InferenceContext<'_> { self.push_diagnostic(InferenceDiagnostic::NoSuchField { field: field.expr.into(), private: false, + variant: def, }); None } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer/pat.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer/pat.rs index 1b354935a5bf0..dac5a5ea6995d 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/infer/pat.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer/pat.rs @@ -177,6 +177,7 @@ impl InferenceContext<'_> { self.push_diagnostic(InferenceDiagnostic::NoSuchField { field: inner.into(), private: true, + variant: def, }); } let f = field_types[local_id].clone(); @@ -190,6 +191,7 @@ impl InferenceContext<'_> { self.push_diagnostic(InferenceDiagnostic::NoSuchField { field: inner.into(), private: false, + variant: def, }); self.err_ty() } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs index 045ffb418c8de..2de1aa30c9660 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs @@ -428,6 +428,17 @@ impl MirEvalError { } Ok(()) } + + pub fn is_panic(&self) -> Option<&str> { + let mut err = self; + while let MirEvalError::InFunction(e, _) = err { + err = e; + } + match err { + MirEvalError::Panic(msg) => Some(msg), + _ => None, + } + } } impl std::fmt::Debug for MirEvalError { @@ -1138,7 +1149,7 @@ impl Evaluator<'_> { let mut ty = self.operand_ty(lhs, locals)?; while let TyKind::Ref(_, _, z) = ty.kind(Interner) { ty = z.clone(); - let size = if ty.kind(Interner) == &TyKind::Str { + let size = if ty.is_str() { if *op != BinOp::Eq { never!("Only eq is builtin for `str`"); } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/shim.rs b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/shim.rs index fee3dd3ada852..3438712049e2d 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/shim.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/shim.rs @@ -49,6 +49,7 @@ impl Evaluator<'_> { if self.not_special_fn_cache.borrow().contains(&def) { return Ok(false); } + let function_data = self.db.function_data(def); let is_intrinsic = match &function_data.abi { Some(abi) => *abi == Interned::new_str("rust-intrinsic"), @@ -131,9 +132,7 @@ impl Evaluator<'_> { return Ok(true); } if let Some(it) = self.detect_lang_function(def) { - let arg_bytes = - args.iter().map(|it| Ok(it.get(self)?.to_owned())).collect::>>()?; - let result = self.exec_lang_item(it, generic_args, &arg_bytes, locals, span)?; + let result = self.exec_lang_item(it, generic_args, args, locals, span)?; destination.write_from_bytes(self, &result)?; return Ok(true); } @@ -311,16 +310,20 @@ impl Evaluator<'_> { fn detect_lang_function(&self, def: FunctionId) -> Option { use LangItem::*; - let candidate = self.db.lang_attr(def.into())?; + let attrs = self.db.attrs(def.into()); + + if attrs.by_key("rustc_const_panic_str").exists() { + // `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE. + return Some(LangItem::BeginPanic); + } + + let candidate = attrs.by_key("lang").string_value().and_then(LangItem::from_str)?; // We want to execute these functions with special logic // `PanicFmt` is not detected here as it's redirected later. if [BeginPanic, SliceLen, DropInPlace].contains(&candidate) { return Some(candidate); } - if self.db.attrs(def.into()).by_key("rustc_const_panic_str").exists() { - // `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE. - return Some(LangItem::BeginPanic); - } + None } @@ -328,18 +331,52 @@ impl Evaluator<'_> { &mut self, it: LangItem, generic_args: &Substitution, - args: &[Vec], + args: &[IntervalAndTy], locals: &Locals, span: MirSpan, ) -> Result> { use LangItem::*; let mut args = args.iter(); match it { - BeginPanic => Err(MirEvalError::Panic("".to_owned())), + BeginPanic => { + let mut arg = args + .next() + .ok_or(MirEvalError::InternalError( + "argument of BeginPanic is not provided".into(), + ))? + .clone(); + while let TyKind::Ref(_, _, ty) = arg.ty.kind(Interner) { + if ty.is_str() { + let (pointee, metadata) = arg.interval.get(self)?.split_at(self.ptr_size()); + let len = from_bytes!(usize, metadata); + + return { + Err(MirEvalError::Panic( + std::str::from_utf8( + self.read_memory(Address::from_bytes(pointee)?, len)?, + ) + .unwrap() + .to_owned(), + )) + }; + } + let size = self.size_of_sized(ty, locals, "begin panic arg")?; + let pointee = arg.interval.get(self)?; + arg = IntervalAndTy { + interval: Interval::new(Address::from_bytes(pointee)?, size), + ty: ty.clone(), + }; + } + Err(MirEvalError::Panic(format!( + "unknown-panic-payload: {:?}", + arg.ty.kind(Interner) + ))) + } SliceLen => { let arg = args.next().ok_or(MirEvalError::InternalError( "argument of <[T]>::len() is not provided".into(), ))?; + let arg = arg.get(self)?; let ptr_size = arg.len() / 2; Ok(arg[ptr_size..].into()) } @@ -353,6 +390,7 @@ impl Evaluator<'_> { let arg = args.next().ok_or(MirEvalError::InternalError( "argument of drop_in_place is not provided".into(), ))?; + let arg = arg.interval.get(self)?.to_owned(); self.run_drop_glue_deep( ty.clone(), locals, diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/tests.rs b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/tests.rs index 381522c9abe62..4abbda56cbb4d 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/tests.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval/tests.rs @@ -31,6 +31,7 @@ fn eval_main(db: &TestDB, file_id: FileId) -> Result<(String, String), MirEvalEr db.trait_environment(func_id.into()), ) .map_err(|e| MirEvalError::MirLowerError(func_id, e))?; + let (result, output) = interpret_mir(db, body, false, None); result?; Ok((output.stdout().into_owned(), output.stderr().into_owned())) @@ -72,6 +73,13 @@ fn check_pass_and_stdio(ra_fixture: &str, expected_stdout: &str, expected_stderr } } +fn check_panic(ra_fixture: &str, expected_panic: &str) { + let (db, file_ids) = TestDB::with_many_files(ra_fixture); + let file_id = *file_ids.last().unwrap(); + let e = eval_main(&db, file_id).unwrap_err(); + assert_eq!(e.is_panic().unwrap_or_else(|| panic!("unexpected error: {:?}", e)), expected_panic); +} + #[test] fn function_with_extern_c_abi() { check_pass( @@ -87,6 +95,43 @@ fn main() { ); } +#[test] +fn panic_fmt() { + // panic! + // -> panic_2021 (builtin macro redirection) + // -> #[lang = "panic_fmt"] core::panicking::panic_fmt (hooked by CTFE for redirection) + // -> core::panicking::const_panic_fmt + // -> #[rustc_const_panic_str] core::panicking::panic_display (hooked by CTFE for builtin panic) + // -> Err(ConstEvalError::Panic) + check_panic( + r#" +//- minicore: fmt, panic +fn main() { + panic!("hello, world!"); +} + "#, + "hello, world!", + ); +} + +#[test] +fn panic_display() { + // panic! + // -> panic_2021 (builtin macro redirection) + // -> #[rustc_const_panic_str] core::panicking::panic_display (hooked by CTFE for builtin panic) + // -> Err(ConstEvalError::Panic) + check_panic( + r#" +//- minicore: fmt, panic + +fn main() { + panic!("{}", "hello, world!"); +} + "#, + "hello, world!", + ); +} + #[test] fn drop_basic() { check_pass( diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/coercion.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/coercion.rs index d56b15b9b741e..526db2af6dccb 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/coercion.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/coercion.rs @@ -186,7 +186,7 @@ fn test() { let x = match 1 { 1 => t as *mut i32, 2 => t as &i32, - //^^^^^^^^^ expected *mut i32, got &i32 + //^^^^^^^^^ expected *mut i32, got &'? i32 _ => t as *const i32, // ^^^^^^^^^^^^^^^ adjustments: Pointer(MutToConstPointer) @@ -307,7 +307,7 @@ fn test() { let foo = Foo; //^^^ type: Foo<{unknown}> let _: &u32 = &Foo; - //^^^^ expected &u32, got &Foo<{unknown}> + //^^^^ expected &'? u32, got &'? Foo<{unknown}> }", ); } @@ -544,9 +544,9 @@ struct Bar(Foo); fn test() { let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; - //^^^^^^^^^^^^^^^^^^^^^ expected &Foo<[usize]>, got &Foo<[i32; 3]> + //^^^^^^^^^^^^^^^^^^^^^ expected &'? Foo<[usize]>, got &'? Foo<[i32; 3]> let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); - //^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &Bar<[usize]>, got &Bar<[i32; 3]> + //^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &'? Bar<[usize]>, got &'? Bar<[i32; 3]> } "#, ); @@ -562,7 +562,7 @@ trait Foo {} fn test(f: impl Foo, g: &(impl Foo + ?Sized)) { let _: &dyn Foo = &f; let _: &dyn Foo = g; - //^ expected &dyn Foo, got &impl Foo + ?Sized + //^ expected &'? dyn Foo, got &'? impl Foo + ?Sized } "#, ); @@ -828,11 +828,11 @@ struct V { t: T } fn main() { let a: V<&dyn Tr>; (a,) = V { t: &S }; - //^^^^expected V<&S>, got (V<&dyn Tr>,) + //^^^^expected V<&'? S>, got (V<&'? dyn Tr>,) let mut a: V<&dyn Tr> = V { t: &S }; (a,) = V { t: &S }; - //^^^^expected V<&S>, got (V<&dyn Tr>,) + //^^^^expected V<&'? S>, got (V<&'? dyn Tr>,) } "#, ); diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/diagnostics.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/diagnostics.rs index 119de7f050e78..def06f2d59d2a 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/diagnostics.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/diagnostics.rs @@ -8,7 +8,7 @@ fn function_return_type_mismatch_1() { r#" fn test() -> &'static str { 5 - //^ expected &str, got i32 + //^ expected &'static str, got i32 } "#, ); @@ -21,7 +21,7 @@ fn function_return_type_mismatch_2() { fn test(x: bool) -> &'static str { if x { return 1; - //^ expected &str, got i32 + //^ expected &'static str, got i32 } "ok" } @@ -38,7 +38,7 @@ fn test(x: bool) -> &'static str { return "ok"; } 1 - //^ expected &str, got i32 + //^ expected &'static str, got i32 } "#, ); @@ -53,7 +53,7 @@ fn test(x: bool) -> &'static str { "ok" } else { 1 - //^ expected &str, got i32 + //^ expected &'static str, got i32 } } "#, @@ -67,7 +67,7 @@ fn function_return_type_mismatch_5() { fn test(x: bool) -> &'static str { if x { 1 - //^ expected &str, got i32 + //^ expected &'static str, got i32 } else { "ok" } @@ -83,10 +83,10 @@ fn non_unit_block_expr_stmt_no_semi() { fn test(x: bool) { if x { "notok" - //^^^^^^^ expected (), got &str + //^^^^^^^ expected (), got &'static str } else { "ok" - //^^^^ expected (), got &str + //^^^^ expected (), got &'static str } match x { true => true, false => 0 } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), got bool diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/display_source_code.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/display_source_code.rs index e8369caa77140..60c03b52246c4 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/display_source_code.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/display_source_code.rs @@ -67,11 +67,11 @@ trait B: A {} fn test( _: &(dyn A + Send), - //^ &(dyn A + Send) + //^ &'_ (dyn A + Send) _: &(dyn Send + A), - //^ &(dyn A + Send) + //^ &'_ (dyn A + Send) _: &dyn B, - //^ &(dyn B) + //^ &'_ (dyn B) ) {} "#, ); @@ -85,7 +85,7 @@ fn render_dyn_for_ty() { trait Foo<'a> {} fn foo(foo: &dyn for<'a> Foo<'a>) {} - // ^^^ &dyn Foo<'_> + // ^^^ &'_ dyn Foo<'_> "#, ); } @@ -111,11 +111,11 @@ fn test( b; //^ impl Foo c; - //^ &impl Foo + ?Sized + //^ &'_ impl Foo + ?Sized d; //^ S ref_any; - //^^^^^^^ &impl ?Sized + //^^^^^^^ &'_ impl ?Sized empty; } //^^^^^ impl Sized "#, @@ -192,7 +192,7 @@ fn test( b; //^ fn(impl Foo) -> impl Foo c; -} //^ fn(&impl Foo + ?Sized) -> &impl Foo + ?Sized +} //^ fn(&'_ impl Foo + ?Sized) -> &'_ impl Foo + ?Sized "#, ); } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/macros.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/macros.rs index 2f75338f99436..a0899cb1d632b 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/macros.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/macros.rs @@ -200,8 +200,8 @@ fn expr_macro_def_expanded_in_various_places() { 100..119 'for _ ...!() {}': IntoIterator::IntoIter 100..119 'for _ ...!() {}': ! 100..119 'for _ ...!() {}': IntoIterator::IntoIter - 100..119 'for _ ...!() {}': &mut IntoIterator::IntoIter - 100..119 'for _ ...!() {}': fn next>(&mut IntoIterator::IntoIter) -> Option< as Iterator>::Item> + 100..119 'for _ ...!() {}': &'? mut IntoIterator::IntoIter + 100..119 'for _ ...!() {}': fn next>(&'? mut IntoIterator::IntoIter) -> Option< as Iterator>::Item> 100..119 'for _ ...!() {}': Option> 100..119 'for _ ...!() {}': () 100..119 'for _ ...!() {}': () @@ -221,7 +221,7 @@ fn expr_macro_def_expanded_in_various_places() { 281..303 'Spam {...m!() }': {unknown} 309..325 'spam!(...am!()]': {unknown} 350..366 'spam!(... usize': usize - 372..380 '&spam!()': &isize + 372..380 '&spam!()': &'? isize 386..394 '-spam!()': isize 400..416 'spam!(...pam!()': {unknown} 422..439 'spam!(...pam!()': isize @@ -293,8 +293,8 @@ fn expr_macro_rules_expanded_in_various_places() { 114..133 'for _ ...!() {}': IntoIterator::IntoIter 114..133 'for _ ...!() {}': ! 114..133 'for _ ...!() {}': IntoIterator::IntoIter - 114..133 'for _ ...!() {}': &mut IntoIterator::IntoIter - 114..133 'for _ ...!() {}': fn next>(&mut IntoIterator::IntoIter) -> Option< as Iterator>::Item> + 114..133 'for _ ...!() {}': &'? mut IntoIterator::IntoIter + 114..133 'for _ ...!() {}': fn next>(&'? mut IntoIterator::IntoIter) -> Option< as Iterator>::Item> 114..133 'for _ ...!() {}': Option> 114..133 'for _ ...!() {}': () 114..133 'for _ ...!() {}': () @@ -314,7 +314,7 @@ fn expr_macro_rules_expanded_in_various_places() { 295..317 'Spam {...m!() }': {unknown} 323..339 'spam!(...am!()]': {unknown} 364..380 'spam!(... usize': usize - 386..394 '&spam!()': &isize + 386..394 '&spam!()': &'? isize 400..408 '-spam!()': isize 414..430 'spam!(...pam!()': {unknown} 436..453 'spam!(...pam!()': isize @@ -539,7 +539,7 @@ fn test() { let msg = foo::Message(foo::MessageRef); let r = msg.deref(); r; - //^ &MessageRef + //^ &'? MessageRef } //- /lib.rs crate:foo @@ -703,9 +703,9 @@ fn infer_builtin_macros_file() { } "#, expect![[r#" - !0..2 '""': &str + !0..2 '""': &'static str 63..87 '{ ...!(); }': () - 73..74 'x': &str + 73..74 'x': &'static str "#]], ); } @@ -741,9 +741,9 @@ fn infer_builtin_macros_concat() { } "#, expect![[r#" - !0..13 '"helloworld!"': &str + !0..13 '"helloworld!"': &'static str 65..121 '{ ...")); }': () - 75..76 'x': &str + 75..76 'x': &'static str "#]], ); } @@ -820,7 +820,7 @@ macro_rules! include_str {() => {}} fn main() { let a = include_str!("foo.rs"); a; -} //^ &str +} //^ &'static str //- /foo.rs hello @@ -847,7 +847,7 @@ macro_rules! m { fn main() { let a = include_str!(m!(".rs")); a; -} //^ &str +} //^ &'static str //- /foo.rs hello @@ -960,9 +960,9 @@ fn infer_builtin_macros_concat_with_lazy() { } "#, expect![[r#" - !0..13 '"helloworld!"': &str + !0..13 '"helloworld!"': &'static str 103..160 '{ ...")); }': () - 113..114 'x': &str + 113..114 'x': &'static str "#]], ); } @@ -977,7 +977,7 @@ fn infer_builtin_macros_env() { fn main() { let x = env!("foo"); - //^ &str + //^ &'static str } "#, ); @@ -991,7 +991,7 @@ fn infer_builtin_macros_option_env() { //- /main.rs env:foo=bar fn main() { let x = option_env!("foo"); - //^ Option<&str> + //^ Option<&'static str> } "#, ); diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs index 8609ba410394f..63a83d403fa97 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs @@ -658,7 +658,7 @@ fn infer_call_trait_method_on_generic_param_1() { } "#, expect![[r#" - 29..33 'self': &Self + 29..33 'self': &'? Self 63..64 't': T 69..88 '{ ...d(); }': () 75..76 't': T @@ -679,7 +679,7 @@ fn infer_call_trait_method_on_generic_param_2() { } "#, expect![[r#" - 32..36 'self': &Self + 32..36 'self': &'? Self 70..71 't': T 76..95 '{ ...d(); }': () 82..83 't': T @@ -757,7 +757,7 @@ struct S; impl Clone for S {} impl Clone for &S {} fn test() { (S.clone(), (&S).clone(), (&&S).clone()); } - //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (S, S, &S) + //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (S, S, &'? S) "#, ); } @@ -1150,12 +1150,12 @@ fn dyn_trait_super_trait_not_in_scope() { } "#, expect![[r#" - 51..55 'self': &Self + 51..55 'self': &'? Self 64..69 '{ 0 }': u32 66..67 '0': u32 - 176..177 'd': &dyn Trait + 176..177 'd': &'? dyn Trait 191..207 '{ ...o(); }': () - 197..198 'd': &dyn Trait + 197..198 'd': &'? dyn Trait 197..204 'd.foo()': u32 "#]], ); @@ -1182,15 +1182,15 @@ fn test() { } "#, expect![[r#" - 75..79 'self': &S + 75..79 'self': &'? S 89..109 '{ ... }': bool 99..103 'true': bool 123..167 '{ ...o(); }': () - 133..134 's': &S - 137..151 'unsafe { f() }': &S - 146..147 'f': fn f() -> &S - 146..149 'f()': &S - 157..158 's': &S + 133..134 's': &'? S + 137..151 'unsafe { f() }': &'static S + 146..147 'f': fn f() -> &'static S + 146..149 'f()': &'static S + 157..158 's': &'? S 157..164 's.foo()': bool "#]], ); @@ -1601,11 +1601,11 @@ use core::IntoIterator; fn f() { let v = [4].into_iter(); v; - //^ &i32 + //^ &'? i32 let a = [0, 1].into_iter(); a; - //^ &i32 + //^ &'? i32 } //- /main2021.rs crate:main2021 deps:core edition:2021 @@ -1618,7 +1618,7 @@ fn f() { let a = [0, 1].into_iter(); a; - //^ &i32 + //^ &'? i32 } //- /core.rs crate:core @@ -1767,7 +1767,7 @@ fn test() { let a2 = A(make(), 1i32); let _: &str = a2.thing(); a2; - //^^ A, i32> + //^^ A, i32> } "#, ); diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/never_type.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/never_type.rs index 5d809b823923c..0ccbcf63e2b67 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/never_type.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/never_type.rs @@ -104,7 +104,7 @@ enum Option { None, Some(T) } fn test() { let a = if true { Option::None } else { Option::Some(return) }; a; - //^ Option<&str> + //^ Option<&'static str> match 42 { 42 => a, _ => Option::Some("str"), @@ -317,8 +317,8 @@ fn diverging_expression_2() { 63..81 '{ loop...foo" }': u32 65..72 'loop {}': ! 70..72 '{}': () - 74..79 '"foo"': &str - 74..79: expected u32, got &str + 74..79 '"foo"': &'static str + 74..79: expected u32, got &'static str "#]], ); } @@ -365,8 +365,8 @@ fn diverging_expression_3_break() { 151..172 'for a ...eak; }': {unknown} 151..172 'for a ...eak; }': ! 151..172 'for a ...eak; }': {unknown} - 151..172 'for a ...eak; }': &mut {unknown} - 151..172 'for a ...eak; }': fn next<{unknown}>(&mut {unknown}) -> Option<<{unknown} as Iterator>::Item> + 151..172 'for a ...eak; }': &'? mut {unknown} + 151..172 'for a ...eak; }': fn next<{unknown}>(&'? mut {unknown}) -> Option<<{unknown} as Iterator>::Item> 151..172 'for a ...eak; }': Option<{unknown}> 151..172 'for a ...eak; }': () 151..172 'for a ...eak; }': () @@ -381,8 +381,8 @@ fn diverging_expression_3_break() { 237..250 'for a in b {}': {unknown} 237..250 'for a in b {}': ! 237..250 'for a in b {}': {unknown} - 237..250 'for a in b {}': &mut {unknown} - 237..250 'for a in b {}': fn next<{unknown}>(&mut {unknown}) -> Option<<{unknown} as Iterator>::Item> + 237..250 'for a in b {}': &'? mut {unknown} + 237..250 'for a in b {}': fn next<{unknown}>(&'? mut {unknown}) -> Option<<{unknown} as Iterator>::Item> 237..250 'for a in b {}': Option<{unknown}> 237..250 'for a in b {}': () 237..250 'for a in b {}': () @@ -396,8 +396,8 @@ fn diverging_expression_3_break() { 315..337 'for a ...urn; }': {unknown} 315..337 'for a ...urn; }': ! 315..337 'for a ...urn; }': {unknown} - 315..337 'for a ...urn; }': &mut {unknown} - 315..337 'for a ...urn; }': fn next<{unknown}>(&mut {unknown}) -> Option<<{unknown} as Iterator>::Item> + 315..337 'for a ...urn; }': &'? mut {unknown} + 315..337 'for a ...urn; }': fn next<{unknown}>(&'? mut {unknown}) -> Option<<{unknown} as Iterator>::Item> 315..337 'for a ...urn; }': Option<{unknown}> 315..337 'for a ...urn; }': () 315..337 'for a ...urn; }': () diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/patterns.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/patterns.rs index 4355881d7299c..5e040a60e29ce 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/patterns.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/patterns.rs @@ -32,27 +32,27 @@ fn infer_pattern() { } "#, expect![[r#" - 8..9 'x': &i32 + 8..9 'x': &'? i32 17..400 '{ ...o_x; }': () - 27..28 'y': &i32 - 31..32 'x': &i32 - 42..44 '&z': &i32 + 27..28 'y': &'? i32 + 31..32 'x': &'? i32 + 42..44 '&z': &'? i32 43..44 'z': i32 - 47..48 'x': &i32 + 47..48 'x': &'? i32 58..59 'a': i32 62..63 'z': i32 - 73..79 '(c, d)': (i32, &str) + 73..79 '(c, d)': (i32, &'static str) 74..75 'c': i32 - 77..78 'd': &str - 82..94 '(1, "hello")': (i32, &str) + 77..78 'd': &'static str + 82..94 '(1, "hello")': (i32, &'static str) 83..84 '1': i32 - 86..93 '"hello"': &str + 86..93 '"hello"': &'static str 101..151 'for (e... }': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter 101..151 'for (e... }': {unknown} 101..151 'for (e... }': ! 101..151 'for (e... }': {unknown} - 101..151 'for (e... }': &mut {unknown} - 101..151 'for (e... }': fn next<{unknown}>(&mut {unknown}) -> Option<<{unknown} as Iterator>::Item> + 101..151 'for (e... }': &'? mut {unknown} + 101..151 'for (e... }': fn next<{unknown}>(&'? mut {unknown}) -> Option<<{unknown} as Iterator>::Item> 101..151 'for (e... }': Option<({unknown}, {unknown})> 101..151 'for (e... }': () 101..151 'for (e... }': () @@ -74,10 +74,10 @@ fn infer_pattern() { 194..197 'val': {unknown} 210..236 'if let...rue {}': () 213..233 'let x ... &true': bool - 217..225 'x @ true': &bool + 217..225 'x @ true': &'? bool 221..225 'true': bool 221..225 'true': bool - 228..233 '&true': &bool + 228..233 '&true': &'? bool 229..233 'true': bool 234..236 '{}': () 246..252 'lambda': impl Fn(u64, u64, i32) -> i32 @@ -90,14 +90,14 @@ fn infer_pattern() { 277..282 'a + b': u64 281..282 'b': u64 284..285 'c': i32 - 298..310 'ref ref_to_x': &&i32 - 313..314 'x': &i32 - 324..333 'mut mut_x': &i32 - 336..337 'x': &i32 - 347..367 'ref mu...f_to_x': &mut &i32 - 370..371 'x': &i32 - 381..382 'k': &mut &i32 - 385..397 'mut_ref_to_x': &mut &i32 + 298..310 'ref ref_to_x': &'? &'? i32 + 313..314 'x': &'? i32 + 324..333 'mut mut_x': &'? i32 + 336..337 'x': &'? i32 + 347..367 'ref mu...f_to_x': &'? mut &'? i32 + 370..371 'x': &'? i32 + 381..382 'k': &'? mut &'? i32 + 385..397 'mut_ref_to_x': &'? mut &'? i32 "#]], ); } @@ -120,14 +120,14 @@ fn infer_literal_pattern() { 17..28 '{ loop {} }': T 19..26 'loop {}': ! 24..26 '{}': () - 37..38 'x': &i32 + 37..38 'x': &'? i32 46..208 '{ ...) {} }': () 52..75 'if let...y() {}': () 55..72 'let "f... any()': bool - 59..64 '"foo"': &str - 59..64 '"foo"': &str - 67..70 'any': fn any<&str>() -> &str - 67..72 'any()': &str + 59..64 '"foo"': &'static str + 59..64 '"foo"': &'static str + 67..70 'any': fn any<&'static str>() -> &'static str + 67..72 'any()': &'static str 73..75 '{}': () 80..99 'if let...y() {}': () 83..96 'let 1 = any()': bool @@ -178,7 +178,7 @@ fn infer_range_pattern() { } "#, expect![[r#" - 8..9 'x': &i32 + 8..9 'x': &'? i32 17..75 '{ ...2 {} }': () 23..45 'if let...u32 {}': () 26..42 'let 1....= 2u32': bool @@ -208,14 +208,14 @@ fn infer_pattern_match_ergonomics() { expect![[r#" 27..78 '{ ...(1); }': () 37..41 'A(n)': A - 39..40 'n': &i32 - 44..49 '&A(1)': &A + 39..40 'n': &'? i32 + 44..49 '&A(1)': &'? A 45..46 'A': extern "rust-call" A(i32) -> A 45..49 'A(1)': A 47..48 '1': i32 59..63 'A(n)': A - 61..62 'n': &mut i32 - 66..75 '&mut A(1)': &mut A + 61..62 'n': &'? mut i32 + 66..75 '&mut A(1)': &'? mut A 71..72 'A': extern "rust-call" A(i32) -> A 71..75 'A(1)': A 73..74 '1': i32 @@ -235,17 +235,17 @@ fn infer_pattern_match_ergonomics_ref() { "#, expect![[r#" 10..56 '{ ...= v; }': () - 20..21 'v': &(i32, &i32) - 24..32 '&(1, &2)': &(i32, &i32) - 25..32 '(1, &2)': (i32, &i32) + 20..21 'v': &'? (i32, &'? i32) + 24..32 '&(1, &2)': &'? (i32, &'? i32) + 25..32 '(1, &2)': (i32, &'? i32) 26..27 '1': i32 - 29..31 '&2': &i32 + 29..31 '&2': &'? i32 30..31 '2': i32 - 42..49 '(_, &w)': (i32, &i32) + 42..49 '(_, &w)': (i32, &'? i32) 43..44 '_': i32 - 46..48 '&w': &i32 + 46..48 '&w': &'? i32 47..48 'w': i32 - 52..53 'v': &(i32, &i32) + 52..53 'v': &'? (i32, &'? i32) "#]], ); } @@ -286,28 +286,28 @@ fn infer_pattern_match_slice() { "#, expect![[r#" 10..209 '{ ... } }': () - 20..25 'slice': &[f64] - 36..42 '&[0.0]': &[f64; 1] + 20..25 'slice': &'? [f64] + 36..42 '&[0.0]': &'? [f64; 1] 37..42 '[0.0]': [f64; 1] 38..41 '0.0': f64 48..207 'match ... }': () - 54..59 'slice': &[f64] - 70..73 '&[]': &[f64] + 54..59 'slice': &'? [f64] + 70..73 '&[]': &'? [f64] 71..73 '[]': [f64] 77..79 '{}': () - 89..93 '&[a]': &[f64] + 89..93 '&[a]': &'? [f64] 90..93 '[a]': [f64] 91..92 'a': f64 97..123 '{ ... }': () 111..112 'a': f64 - 133..140 '&[b, c]': &[f64] + 133..140 '&[b, c]': &'? [f64] 134..140 '[b, c]': [f64] 135..136 'b': f64 138..139 'c': f64 144..185 '{ ... }': () 158..159 'b': f64 173..174 'c': f64 - 194..195 '_': &[f64] + 194..195 '_': &'? [f64] 199..201 '{}': () "#]], ); @@ -327,14 +327,14 @@ fn infer_pattern_match_string_literal() { "#, expect![[r#" 10..98 '{ ... } }': () - 20..21 's': &str - 30..37 '"hello"': &str + 20..21 's': &'? str + 30..37 '"hello"': &'static str 43..96 'match ... }': () - 49..50 's': &str - 61..68 '"hello"': &str - 61..68 '"hello"': &str + 49..50 's': &'? str + 61..68 '"hello"': &'static str + 61..68 '"hello"': &'static str 72..74 '{}': () - 83..84 '_': &str + 83..84 '_': &'? str 88..90 '{}': () "#]], ); @@ -358,27 +358,27 @@ fn infer_pattern_match_byte_string_literal() { } "#, expect![[r#" - 105..109 'self': &[T; N] + 105..109 'self': &'? [T; N] 111..116 'index': {unknown} - 157..180 '{ ... }': &[u8] + 157..180 '{ ... }': &'? [u8] 167..174 'loop {}': ! 172..174 '{}': () 191..192 'v': [u8; 3] 203..261 '{ ...v {} }': () 209..233 'if let...[S] {}': () 212..230 'let b"... &v[S]': bool - 216..222 'b"foo"': &[u8] - 216..222 'b"foo"': &[u8] - 225..230 '&v[S]': &[u8] + 216..222 'b"foo"': &'static [u8] + 216..222 'b"foo"': &'static [u8] + 225..230 '&v[S]': &'? [u8] 226..227 'v': [u8; 3] 226..230 'v[S]': [u8] 228..229 'S': S 231..233 '{}': () 238..259 'if let... &v {}': () 241..256 'let b"foo" = &v': bool - 245..251 'b"foo"': &[u8; 3] - 245..251 'b"foo"': &[u8; 3] - 254..256 '&v': &[u8; 3] + 245..251 'b"foo"': &'static [u8; 3] + 245..251 'b"foo"': &'static [u8; 3] + 254..256 '&v': &'? [u8; 3] 255..256 'v': [u8; 3] 257..259 '{}': () "#]], @@ -399,17 +399,17 @@ fn infer_pattern_match_or() { "#, expect![[r#" 10..108 '{ ... } }': () - 20..21 's': &str - 30..37 '"hello"': &str + 20..21 's': &'? str + 30..37 '"hello"': &'static str 43..106 'match ... }': () - 49..50 's': &str - 61..68 '"hello"': &str - 61..68 '"hello"': &str - 61..78 '"hello...world"': &str - 71..78 '"world"': &str - 71..78 '"world"': &str + 49..50 's': &'? str + 61..68 '"hello"': &'static str + 61..68 '"hello"': &'static str + 61..78 '"hello...world"': &'? str + 71..78 '"world"': &'static str + 71..78 '"world"': &'static str 82..84 '{}': () - 93..94 '_': &str + 93..94 '_': &'? str 98..100 '{}': () "#]], ); @@ -505,10 +505,10 @@ fn infer_adt_pattern() { 216..217 '1': usize 227..231 'E::B': E 235..237 '10': usize - 255..274 'ref d ...{ .. }': &E + 255..274 'ref d ...{ .. }': &'? E 263..274 'E::A { .. }': E 277..278 'e': E - 284..285 'd': &E + 284..285 'd': &'? E "#]], ); } @@ -529,14 +529,14 @@ impl Foo { expect![[r#" 42..151 '{ ... }': () 56..64 'Self(s,)': Foo - 61..62 's': &usize - 67..75 '&Foo(0,)': &Foo + 61..62 's': &'? usize + 67..75 '&Foo(0,)': &'? Foo 68..71 'Foo': extern "rust-call" Foo(usize) -> Foo 68..75 'Foo(0,)': Foo 72..73 '0': usize 89..97 'Self(s,)': Foo - 94..95 's': &mut usize - 100..112 '&mut Foo(0,)': &mut Foo + 94..95 's': &'? mut usize + 100..112 '&mut Foo(0,)': &'? mut Foo 105..108 'Foo': extern "rust-call" Foo(usize) -> Foo 105..112 'Foo(0,)': Foo 109..110 '0': usize @@ -669,7 +669,7 @@ fn main() { } "#, expect![[r#" - 27..31 'self': &S + 27..31 'self': &'? S 41..50 '{ false }': bool 43..48 'false': bool 64..115 '{ ... } }': () @@ -679,7 +679,7 @@ fn main() { 93..94 's': S 93..100 's.foo()': bool 104..106 '()': () - "#]], + "#]], ) } @@ -702,29 +702,29 @@ fn test() { 51..58 'loop {}': ! 56..58 '{}': () 72..171 '{ ... x); }': () - 78..81 'foo': fn foo<&(i32, &str), i32, impl FnOnce(&(i32, &str)) -> i32>(&(i32, &str), impl FnOnce(&(i32, &str)) -> i32) -> i32 + 78..81 'foo': fn foo<&'? (i32, &'? str), i32, impl FnOnce(&'? (i32, &'? str)) -> i32>(&'? (i32, &'? str), impl FnOnce(&'? (i32, &'? str)) -> i32) -> i32 78..105 'foo(&(...y)| x)': i32 - 82..91 '&(1, "a")': &(i32, &str) - 83..91 '(1, "a")': (i32, &str) + 82..91 '&(1, "a")': &'? (i32, &'static str) + 83..91 '(1, "a")': (i32, &'static str) 84..85 '1': i32 - 87..90 '"a"': &str - 93..104 '|&(x, y)| x': impl FnOnce(&(i32, &str)) -> i32 - 94..101 '&(x, y)': &(i32, &str) - 95..101 '(x, y)': (i32, &str) + 87..90 '"a"': &'static str + 93..104 '|&(x, y)| x': impl FnOnce(&'? (i32, &'? str)) -> i32 + 94..101 '&(x, y)': &'? (i32, &'? str) + 95..101 '(x, y)': (i32, &'? str) 96..97 'x': i32 - 99..100 'y': &str + 99..100 'y': &'? str 103..104 'x': i32 - 142..145 'foo': fn foo<&(i32, &str), &i32, impl FnOnce(&(i32, &str)) -> &i32>(&(i32, &str), impl FnOnce(&(i32, &str)) -> &i32) -> &i32 - 142..168 'foo(&(...y)| x)': &i32 - 146..155 '&(1, "a")': &(i32, &str) - 147..155 '(1, "a")': (i32, &str) + 142..145 'foo': fn foo<&'? (i32, &'? str), &'? i32, impl FnOnce(&'? (i32, &'? str)) -> &'? i32>(&'? (i32, &'? str), impl FnOnce(&'? (i32, &'? str)) -> &'? i32) -> &'? i32 + 142..168 'foo(&(...y)| x)': &'? i32 + 146..155 '&(1, "a")': &'? (i32, &'static str) + 147..155 '(1, "a")': (i32, &'static str) 148..149 '1': i32 - 151..154 '"a"': &str - 157..167 '|(x, y)| x': impl FnOnce(&(i32, &str)) -> &i32 - 158..164 '(x, y)': (i32, &str) - 159..160 'x': &i32 - 162..163 'y': &&str - 166..167 'x': &i32 + 151..154 '"a"': &'static str + 157..167 '|(x, y)| x': impl FnOnce(&'? (i32, &'? str)) -> &'? i32 + 158..164 '(x, y)': (i32, &'? str) + 159..160 'x': &'? i32 + 162..163 'y': &'? &'? str + 166..167 'x': &'? i32 "#]], ); } @@ -741,13 +741,13 @@ fn slice_tail_pattern() { } "#, expect![[r#" - 7..13 'params': &[i32] + 7..13 'params': &'? [i32] 23..92 '{ ... } }': () 29..90 'match ... }': () - 35..41 'params': &[i32] + 35..41 'params': &'? [i32] 52..69 '[head,... @ ..]': [i32] - 53..57 'head': &i32 - 59..68 'tail @ ..': &[i32] + 53..57 'head': &'? i32 + 59..68 'tail @ ..': &'? [i32] 66..68 '..': [i32] 73..84 '{ }': () "#]], @@ -1109,7 +1109,7 @@ fn var_args() { #[lang = "va_list"] pub struct VaListImpl<'f>; fn my_fn(foo: ...) {} - //^^^ VaListImpl<'{error}> + //^^^ VaListImpl<'?> "#, ); } @@ -1122,9 +1122,9 @@ fn foo() { let &() = &(); let &mut () = &mut (); let &mut () = &(); - //^^^^^^^ expected &(), got &mut () + //^^^^^^^ expected &'? (), got &'? mut () let &() = &mut (); - //^^^ expected &mut (), got &() + //^^^ expected &'? mut (), got &'? () } "#, ); @@ -1148,7 +1148,7 @@ fn main() { }; if let Wrap::::A { cool, ..} = &wrapped {} - //^^^^ &u32 + //^^^^ &'? u32 } "#, ); @@ -1182,7 +1182,7 @@ fn main() { }; if let Wrap::<::Props>::A { cool, ..} = &wrapped {} - //^^^^ &u32 + //^^^^ &'? u32 } "#, ); @@ -1217,7 +1217,7 @@ fn main() { match &6 { Foo::::TEST_I32_REF => (), Foo::::TEST_I32 => (), - //^^^^^^^^^^^^^^^^^^^^ expected &i32, got i32 + //^^^^^^^^^^^^^^^^^^^^ expected &'? i32, got i32 _ => (), } } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs index c2d2047e6f99b..43fe14126e0d8 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs @@ -99,7 +99,7 @@ fn recursive_vars() { 24..31 'unknown': {unknown} 37..44 '[y, &y]': [{unknown}; 2] 38..39 'y': {unknown} - 41..43 '&y': &{unknown} + 41..43 '&y': &'? {unknown} 42..43 'y': {unknown} "#]], ); @@ -117,19 +117,19 @@ fn recursive_vars_2() { "#, expect![[r#" 10..79 '{ ...x)]; }': () - 20..21 'x': &{unknown} - 24..31 'unknown': &{unknown} + 20..21 'x': &'? {unknown} + 24..31 'unknown': &'? {unknown} 41..42 'y': {unknown} 45..52 'unknown': {unknown} - 58..76 '[(x, y..., &x)]': [(&{unknown}, {unknown}); 2] - 59..65 '(x, y)': (&{unknown}, {unknown}) - 60..61 'x': &{unknown} + 58..76 '[(x, y..., &x)]': [(&'? {unknown}, {unknown}); 2] + 59..65 '(x, y)': (&'? {unknown}, {unknown}) + 60..61 'x': &'? {unknown} 63..64 'y': {unknown} - 67..75 '(&y, &x)': (&{unknown}, {unknown}) - 68..70 '&y': &{unknown} + 67..75 '(&y, &x)': (&'? {unknown}, {unknown}) + 68..70 '&y': &'? {unknown} 69..70 'y': {unknown} - 72..74 '&x': &&{unknown} - 73..74 'x': &{unknown} + 72..74 '&x': &'? &'? {unknown} + 73..74 'x': &'? {unknown} "#]], ); } @@ -166,7 +166,7 @@ fn infer_std_crash_1() { 59..136 'match ... }': () 65..82 'someth...nknown': Maybe<{unknown}> 93..123 'Maybe:...thing)': Maybe<{unknown}> - 105..122 'ref mu...ething': &mut {unknown} + 105..122 'ref mu...ething': &'? mut {unknown} 127..129 '()': () "#]], ); @@ -183,7 +183,7 @@ fn infer_std_crash_2() { "#, expect![[r#" 22..52 '{ ...n']; }': () - 28..49 '&[0, b...b'\n']': &[u8; 4] + 28..49 '&[0, b...b'\n']': &'? [u8; 4] 29..49 '[0, b'...b'\n']': [u8; 4] 30..31 '0': u8 33..38 'b'\n'': u8 @@ -269,8 +269,8 @@ fn infer_std_crash_5() { 32..320 'for co... }': {unknown} 32..320 'for co... }': ! 32..320 'for co... }': {unknown} - 32..320 'for co... }': &mut {unknown} - 32..320 'for co... }': fn next<{unknown}>(&mut {unknown}) -> Option<<{unknown} as Iterator>::Item> + 32..320 'for co... }': &'? mut {unknown} + 32..320 'for co... }': fn next<{unknown}>(&'? mut {unknown}) -> Option<<{unknown} as Iterator>::Item> 32..320 'for co... }': Option<{unknown}> 32..320 'for co... }': () 32..320 'for co... }': () @@ -278,22 +278,22 @@ fn infer_std_crash_5() { 36..43 'content': {unknown} 47..60 'doesnt_matter': {unknown} 61..320 '{ ... }': () - 75..79 'name': &{unknown} - 82..166 'if doe... }': &{unknown} + 75..79 'name': &'? {unknown} + 82..166 'if doe... }': &'? {unknown} 85..98 'doesnt_matter': bool - 99..128 '{ ... }': &{unknown} - 113..118 'first': &{unknown} - 134..166 '{ ... }': &{unknown} - 148..156 '&content': &{unknown} + 99..128 '{ ... }': &'? {unknown} + 113..118 'first': &'? {unknown} + 134..166 '{ ... }': &'? {unknown} + 148..156 '&content': &'? {unknown} 149..156 'content': {unknown} - 181..188 'content': &{unknown} - 191..313 'if ICE... }': &{unknown} + 181..188 'content': &'? {unknown} + 191..313 'if ICE... }': &'? {unknown} 194..231 'ICE_RE..._VALUE': {unknown} 194..247 'ICE_RE...&name)': bool - 241..246 '&name': &&{unknown} - 242..246 'name': &{unknown} - 248..276 '{ ... }': &{unknown} - 262..266 'name': &{unknown} + 241..246 '&name': &'? &'? {unknown} + 242..246 'name': &'? {unknown} + 248..276 '{ ... }': &'? {unknown} + 262..266 'name': &'? {unknown} 282..313 '{ ... }': {unknown} 296..303 'content': {unknown} "#]], @@ -318,7 +318,7 @@ fn infer_nested_generics_crash() { expect![[r#" 91..105 'query_response': Canonical> 136..166 '{ ...lue; }': () - 142..163 '&query....value': &QueryResponse + 142..163 '&query....value': &'? QueryResponse 143..157 'query_response': Canonical> 143..163 'query_....value': QueryResponse "#]], @@ -465,12 +465,12 @@ fn issue_3999_slice() { } "#, expect![[r#" - 7..13 'params': &[usize] + 7..13 'params': &'? [usize] 25..80 '{ ... } }': () 31..78 'match ... }': () - 37..43 'params': &[usize] + 37..43 'params': &'? [usize] 54..66 '[ps @ .., _]': [usize] - 55..62 'ps @ ..': &[usize] + 55..62 'ps @ ..': &'? [usize] 60..62 '..': [usize] 64..65 '_': usize 70..72 '{}': () @@ -523,13 +523,13 @@ fn issue_4235_name_conflicts() { "#, expect![[r#" 31..37 'FOO {}': FOO - 63..67 'self': &FOO + 63..67 'self': &'? FOO 69..71 '{}': () 85..119 '{ ...o(); }': () - 95..96 'a': &FOO - 99..103 '&FOO': &FOO + 95..96 'a': &'? FOO + 99..103 '&FOO': &'? FOO 100..103 'FOO': FOO - 109..110 'a': &FOO + 109..110 'a': &'? FOO 109..116 'a.foo()': () "#]], ); @@ -715,12 +715,12 @@ fn issue_4885() { } "#, expect![[r#" - 70..73 'key': &K + 70..73 'key': &'? K 132..148 '{ ...key) }': impl Future>::Bar> - 138..141 'bar': fn bar(&K) -> impl Future>::Bar> + 138..141 'bar': fn bar(&'? K) -> impl Future>::Bar> 138..146 'bar(key)': impl Future>::Bar> - 142..145 'key': &K - 162..165 'key': &K + 142..145 'key': &'? K + 162..165 'key': &'? K 224..227 '{ }': () "#]], ); @@ -771,11 +771,11 @@ fn issue_4800() { } "#, expect![[r#" - 379..383 'self': &mut PeerSet + 379..383 'self': &'? mut PeerSet 401..424 '{ ... }': dyn Future 411..418 'loop {}': ! 416..418 '{}': () - 575..579 'self': &mut Self + 575..579 'self': &'? mut Self "#]], ); } @@ -815,19 +815,19 @@ fn issue_4966() { 225..229 'iter': T 244..246 '{}': Vec 258..402 '{ ...r(); }': () - 268..273 'inner': Map f64> - 276..300 'Map { ... 0.0 }': Map f64> - 285..298 '|_: &f64| 0.0': impl Fn(&f64) -> f64 - 286..287 '_': &f64 + 268..273 'inner': Map f64> + 276..300 'Map { ... 0.0 }': Map f64> + 285..298 '|_: &f64| 0.0': impl Fn(&'? f64) -> f64 + 286..287 '_': &'? f64 295..298 '0.0': f64 - 311..317 'repeat': Repeat f64>> - 320..345 'Repeat...nner }': Repeat f64>> - 338..343 'inner': Map f64> - 356..359 'vec': Vec f64>>>> - 362..371 'from_iter': fn from_iter f64>>>, Repeat f64>>>(Repeat f64>>) -> Vec f64>>>> - 362..379 'from_i...epeat)': Vec f64>>>> - 372..378 'repeat': Repeat f64>> - 386..389 'vec': Vec f64>>>> + 311..317 'repeat': Repeat f64>> + 320..345 'Repeat...nner }': Repeat f64>> + 338..343 'inner': Map f64> + 356..359 'vec': Vec f64>>>> + 362..371 'from_iter': fn from_iter f64>>>, Repeat f64>>>(Repeat f64>>) -> Vec f64>>>> + 362..379 'from_i...epeat)': Vec f64>>>> + 372..378 'repeat': Repeat f64>> + 386..389 'vec': Vec f64>>>> 386..399 'vec.foo_bar()': {unknown} "#]], ); @@ -850,10 +850,10 @@ fn main() { } "#, expect![[r#" - 40..44 'self': &S + 40..44 'self': &'? S 46..48 '_t': T 53..55 '{}': () - 81..85 'self': &S + 81..85 'self': &'? S 87..89 '_f': F 94..96 '{}': () 109..160 '{ ...10); }': () @@ -862,8 +862,8 @@ fn main() { 123..126 'S()': S 132..133 's': S 132..144 's.g(|_x| {})': () - 136..143 '|_x| {}': impl FnOnce(&i32) - 137..139 '_x': &i32 + 136..143 '|_x| {}': impl FnOnce(&'? i32) + 137..139 '_x': &'? i32 141..143 '{}': () 150..151 's': S 150..157 's.f(10)': () @@ -895,14 +895,14 @@ fn flush(&self) { } "#, expect![[r#" - 123..127 'self': &Mutex - 150..152 '{}': MutexGuard<'{error}, T> - 234..238 'self': &{unknown} + 123..127 'self': &'? Mutex + 150..152 '{}': MutexGuard<'?, T> + 234..238 'self': &'? {unknown} 240..290 '{ ...()); }': () - 250..251 'w': &Mutex + 250..251 'w': &'? Mutex 276..287 '*(w.lock())': BufWriter - 278..279 'w': &Mutex - 278..286 'w.lock()': MutexGuard<'{error}, BufWriter> + 278..279 'w': &'? Mutex + 278..286 'w.lock()': MutexGuard<'?, BufWriter> "#]], ); } @@ -1023,20 +1023,20 @@ fn cfg_tail() { expect![[r#" 14..53 '{ ...)] 9 }': () 20..31 '{ "first" }': () - 22..29 '"first"': &str + 22..29 '"first"': &'static str 72..190 '{ ...] 13 }': () 78..88 '{ "fake" }': () - 80..86 '"fake"': &str + 80..86 '"fake"': &'static str 93..103 '{ "fake" }': () - 95..101 '"fake"': &str + 95..101 '"fake"': &'static str 108..120 '{ "second" }': () - 110..118 '"second"': &str + 110..118 '"second"': &'static str 210..273 '{ ... 15; }': () 216..227 '{ "third" }': () - 218..225 '"third"': &str + 218..225 '"third"': &'static str 293..357 '{ ...] 15 }': () - 299..311 '{ "fourth" }': &str - 301..309 '"fourth"': &str + 299..311 '{ "fourth" }': &'static str + 301..309 '"fourth"': &'static str "#]], ) } @@ -1238,8 +1238,8 @@ fn test() { 16..66 'for _ ... }': IntoIterator::IntoIter<()> 16..66 'for _ ... }': ! 16..66 'for _ ... }': IntoIterator::IntoIter<()> - 16..66 'for _ ... }': &mut IntoIterator::IntoIter<()> - 16..66 'for _ ... }': fn next>(&mut IntoIterator::IntoIter<()>) -> Option< as Iterator>::Item> + 16..66 'for _ ... }': &'? mut IntoIterator::IntoIter<()> + 16..66 'for _ ... }': fn next>(&'? mut IntoIterator::IntoIter<()>) -> Option< as Iterator>::Item> 16..66 'for _ ... }': Option> 16..66 'for _ ... }': () 16..66 'for _ ... }': () @@ -1727,7 +1727,7 @@ fn dyn_with_unresolved_trait() { r#" fn foo(a: &dyn DoesNotExist) { a.bar(); - //^&{unknown} + //^&'? {unknown} } "#, ); @@ -1851,9 +1851,9 @@ fn foo() { match &E::A { b @ (x @ E::A | x) => { b; - //^ &E + //^ &'? E x; - //^ &E + //^ &'? E } } }", diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/simple.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/simple.rs index a9d28ebfef570..e2cd7fa26b224 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/simple.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/simple.rs @@ -115,15 +115,15 @@ fn test(a: u32, b: isize, c: !, d: &str) { 8..9 'a': u32 16..17 'b': isize 26..27 'c': ! - 32..33 'd': &str + 32..33 'd': &'? str 41..120 '{ ...f32; }': () 47..48 'a': u32 54..55 'b': isize 61..62 'c': ! - 68..69 'd': &str + 68..69 'd': &'? str 75..81 '1usize': usize 87..93 '1isize': isize - 99..105 '"test"': &str + 99..105 '"test"': &'static str 111..117 '1.0f32': f32 "#]], ); @@ -344,23 +344,23 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { } "#, expect![[r#" - 8..9 'a': &u32 - 17..18 'b': &mut u32 + 8..9 'a': &'? u32 + 17..18 'b': &'? mut u32 30..31 'c': *const u32 45..46 'd': *mut u32 58..149 '{ ... *d; }': () - 64..65 'a': &u32 + 64..65 'a': &'? u32 71..73 '*a': u32 - 72..73 'a': &u32 - 79..81 '&a': &&u32 - 80..81 'a': &u32 - 87..93 '&mut a': &mut &u32 - 92..93 'a': &u32 - 99..100 'b': &mut u32 + 72..73 'a': &'? u32 + 79..81 '&a': &'? &'? u32 + 80..81 'a': &'? u32 + 87..93 '&mut a': &'? mut &'? u32 + 92..93 'a': &'? u32 + 99..100 'b': &'? mut u32 106..108 '*b': u32 - 107..108 'b': &mut u32 - 114..116 '&b': &&mut u32 - 115..116 'b': &mut u32 + 107..108 'b': &'? mut u32 + 114..116 '&b': &'? &'? mut u32 + 115..116 'b': &'? mut u32 122..123 'c': *const u32 129..131 '*c': u32 130..131 'c': *const u32 @@ -425,22 +425,22 @@ h"; 32..36 '5i32': i32 50..54 '5f32': f32 68..72 '5f64': f64 - 86..93 '"hello"': &str - 107..115 'b"bytes"': &[u8; 5] + 86..93 '"hello"': &'static str + 107..115 'b"bytes"': &'static [u8; 5] 129..132 ''c'': char 146..150 'b'b'': u8 164..168 '3.14': f64 182..186 '5000': i32 200..205 'false': bool 219..223 'true': bool - 237..333 'r#" ... "#': &str - 347..357 'br#"yolo"#': &[u8; 4] - 375..376 'a': &[u8; 4] - 379..403 'b"a\x2... c"': &[u8; 4] - 421..422 'b': &[u8; 4] - 425..433 'br"g\ h"': &[u8; 4] - 451..452 'c': &[u8; 6] - 455..467 'br#"x"\"yb"#': &[u8; 6] + 237..333 'r#" ... "#': &'static str + 347..357 'br#"yolo"#': &'static [u8; 4] + 375..376 'a': &'static [u8; 4] + 379..403 'b"a\x2... c"': &'static [u8; 4] + 421..422 'b': &'static [u8; 4] + 425..433 'br"g\ h"': &'static [u8; 4] + 451..452 'c': &'static [u8; 6] + 455..467 'br#"x"\"yb"#': &'static [u8; 6] "##]], ); } @@ -508,9 +508,9 @@ fn test(x: SomeType) { 238..240 '!x': {unknown} 239..240 'x': SomeType 246..254 '-"hello"': {unknown} - 247..254 '"hello"': &str + 247..254 '"hello"': &'static str 260..268 '!"hello"': {unknown} - 261..268 '"hello"': &str + 261..268 '"hello"': &'static str "#]], ); } @@ -535,7 +535,7 @@ fn test() -> &mut &f64 { expect![[r#" 13..14 'x': u32 21..23 '{}': () - 77..230 '{ ...t &c }': &mut &f64 + 77..230 '{ ...t &c }': &'? mut &'? f64 87..88 'a': u32 91..107 'unknow...nction': {unknown} 91..109 'unknow...tion()': u32 @@ -550,8 +550,8 @@ fn test() -> &mut &f64 { 193..194 'c': f64 197..213 'unknow...nction': {unknown} 197..215 'unknow...tion()': f64 - 221..228 '&mut &c': &mut &f64 - 226..228 '&c': &f64 + 221..228 '&mut &c': &'? mut &'? f64 + 226..228 '&c': &'? f64 227..228 'c': f64 "#]], ); @@ -579,12 +579,12 @@ impl S { } "#, expect![[r#" - 33..37 'self': &S + 33..37 'self': &'? S 39..60 '{ ... }': () - 49..53 'self': &S - 74..78 'self': &S + 49..53 'self': &'? S + 74..78 'self': &'? S 87..108 '{ ... }': () - 97..101 'self': &S + 97..101 'self': &'? S 132..152 '{ ... }': S 142..146 'S {}': S 176..199 '{ ... }': S @@ -771,35 +771,35 @@ fn test2(a1: *const A, a2: *mut A) { 64..65 'a': A 71..73 'a1': A 71..75 'a1.b': B - 85..87 'a2': &A - 90..92 '&a': &A + 85..87 'a2': &'? A + 90..92 '&a': &'? A 91..92 'a': A - 98..100 'a2': &A + 98..100 'a2': &'? A 98..102 'a2.b': B - 112..114 'a3': &mut A - 117..123 '&mut a': &mut A + 112..114 'a3': &'? mut A + 117..123 '&mut a': &'? mut A 122..123 'a': A - 129..131 'a3': &mut A + 129..131 'a3': &'? mut A 129..133 'a3.b': B - 143..145 'a4': &&&&&&&A - 148..156 '&&&&&&&a': &&&&&&&A - 149..156 '&&&&&&a': &&&&&&A - 150..156 '&&&&&a': &&&&&A - 151..156 '&&&&a': &&&&A - 152..156 '&&&a': &&&A - 153..156 '&&a': &&A - 154..156 '&a': &A + 143..145 'a4': &'? &'? &'? &'? &'? &'? &'? A + 148..156 '&&&&&&&a': &'? &'? &'? &'? &'? &'? &'? A + 149..156 '&&&&&&a': &'? &'? &'? &'? &'? &'? A + 150..156 '&&&&&a': &'? &'? &'? &'? &'? A + 151..156 '&&&&a': &'? &'? &'? &'? A + 152..156 '&&&a': &'? &'? &'? A + 153..156 '&&a': &'? &'? A + 154..156 '&a': &'? A 155..156 'a': A - 162..164 'a4': &&&&&&&A + 162..164 'a4': &'? &'? &'? &'? &'? &'? &'? A 162..166 'a4.b': B - 176..178 'a5': &mut &&mut &&mut A - 181..199 '&mut &...&mut a': &mut &&mut &&mut A - 186..199 '&&mut &&mut a': &&mut &&mut A - 187..199 '&mut &&mut a': &mut &&mut A - 192..199 '&&mut a': &&mut A - 193..199 '&mut a': &mut A + 176..178 'a5': &'? mut &'? &'? mut &'? &'? mut A + 181..199 '&mut &...&mut a': &'? mut &'? &'? mut &'? &'? mut A + 186..199 '&&mut &&mut a': &'? &'? mut &'? &'? mut A + 187..199 '&mut &&mut a': &'? mut &'? &'? mut A + 192..199 '&&mut a': &'? &'? mut A + 193..199 '&mut a': &'? mut A 198..199 'a': A - 205..207 'a5': &mut &&mut &&mut A + 205..207 'a5': &'? mut &'? &'? mut &'? &'? mut A 205..209 'a5.b': B 223..225 'a1': *const A 237..239 'a2': *mut A @@ -840,22 +840,22 @@ fn test() { } "#, expect![[r#" - 66..70 'self': &A - 78..101 '{ ... }': &T - 88..95 '&self.0': &T - 89..93 'self': &A + 66..70 'self': &'? A + 78..101 '{ ... }': &'? T + 88..95 '&self.0': &'? T + 89..93 'self': &'? A 89..95 'self.0': T - 182..186 'self': &B - 205..228 '{ ... }': &T - 215..222 '&self.0': &T - 216..220 'self': &B + 182..186 'self': &'? B + 205..228 '{ ... }': &'? T + 215..222 '&self.0': &'? T + 216..220 'self': &'? B 216..222 'self.0': T 242..280 '{ ...))); }': () - 252..253 't': &i32 - 256..262 'A::foo': fn foo(&A) -> &i32 - 256..277 'A::foo...42))))': &i32 - 263..276 '&&B(B(A(42)))': &&B>> - 264..276 '&B(B(A(42)))': &B>> + 252..253 't': &'? i32 + 256..262 'A::foo': fn foo(&'? A) -> &'? i32 + 256..277 'A::foo...42))))': &'? i32 + 263..276 '&&B(B(A(42)))': &'? &'? B>> + 264..276 '&B(B(A(42)))': &'? B>> 265..266 'B': extern "rust-call" B>>(B>) -> B>> 265..276 'B(B(A(42)))': B>> 267..268 'B': extern "rust-call" B>(A) -> B> @@ -895,28 +895,28 @@ fn test(a: A) { } "#, expect![[r#" - 71..75 'self': &A - 77..78 'x': &A - 93..114 '{ ... }': &T - 103..108 '&*x.0': &T + 71..75 'self': &'? A + 77..78 'x': &'? A + 93..114 '{ ... }': &'? T + 103..108 '&*x.0': &'? T 104..108 '*x.0': T - 105..106 'x': &A + 105..106 'x': &'? A 105..108 'x.0': *mut T - 195..199 'self': &B - 218..241 '{ ... }': &T - 228..235 '&self.0': &T - 229..233 'self': &B + 195..199 'self': &'? B + 218..241 '{ ... }': &'? T + 228..235 '&self.0': &'? T + 229..233 'self': &'? B 229..235 'self.0': T 253..254 'a': A 264..310 '{ ...))); }': () - 274..275 't': &i32 + 274..275 't': &'? i32 278..279 'A': extern "rust-call" A(*mut i32) -> A 278..292 'A(0 as *mut _)': A - 278..307 'A(0 as...B(a)))': &i32 + 278..307 'A(0 as...B(a)))': &'? i32 280..281 '0': i32 280..291 '0 as *mut _': *mut i32 - 297..306 '&&B(B(a))': &&B>> - 298..306 '&B(B(a))': &B>> + 297..306 '&&B(B(a))': &'? &'? B>> + 298..306 '&B(B(a))': &'? B>> 299..300 'B': extern "rust-call" B>>(B>) -> B>> 299..306 'B(B(a))': B>> 301..302 'B': extern "rust-call" B>(A) -> B> @@ -1044,7 +1044,7 @@ fn infer_inherent_method() { 31..35 'self': A 37..38 'x': u32 52..54 '{}': i32 - 106..110 'self': &A + 106..110 'self': &'? A 112..113 'x': u64 127..129 '{}': i64 147..148 'a': A @@ -1053,7 +1053,7 @@ fn infer_inherent_method() { 159..167 'a.foo(1)': i32 165..166 '1': u32 173..184 '(&a).bar(1)': i64 - 174..176 '&a': &A + 174..176 '&a': &'? A 175..176 'a': A 182..183 '1': u64 190..191 'a': A @@ -1078,10 +1078,10 @@ fn test() { } "#, expect![[r#" - 67..71 'self': &str + 67..71 'self': &'? str 80..82 '{}': i32 96..116 '{ ...o(); }': () - 102..107 '"foo"': &str + 102..107 '"foo"': &'static str 102..113 '"foo".foo()': i32 "#]], ); @@ -1101,33 +1101,33 @@ fn infer_tuple() { } "#, expect![[r#" - 8..9 'x': &str + 8..9 'x': &'? str 17..18 'y': isize 27..169 '{ ...d"); }': () - 37..38 'a': (u32, &str) - 54..62 '(1, "a")': (u32, &str) + 37..38 'a': (u32, &'? str) + 54..62 '(1, "a")': (u32, &'? str) 55..56 '1': u32 - 58..61 '"a"': &str - 72..73 'b': ((u32, &str), &str) - 76..82 '(a, x)': ((u32, &str), &str) - 77..78 'a': (u32, &str) - 80..81 'x': &str - 92..93 'c': (isize, &str) - 96..102 '(y, x)': (isize, &str) + 58..61 '"a"': &'static str + 72..73 'b': ((u32, &'? str), &'? str) + 76..82 '(a, x)': ((u32, &'? str), &'? str) + 77..78 'a': (u32, &'? str) + 80..81 'x': &'? str + 92..93 'c': (isize, &'? str) + 96..102 '(y, x)': (isize, &'? str) 97..98 'y': isize - 100..101 'x': &str - 112..113 'd': ((isize, &str), &str) - 116..122 '(c, x)': ((isize, &str), &str) - 117..118 'c': (isize, &str) - 120..121 'x': &str - 132..133 'e': (i32, &str) - 136..144 '(1, "e")': (i32, &str) + 100..101 'x': &'? str + 112..113 'd': ((isize, &'? str), &'? str) + 116..122 '(c, x)': ((isize, &'? str), &'? str) + 117..118 'c': (isize, &'? str) + 120..121 'x': &'? str + 132..133 'e': (i32, &'static str) + 136..144 '(1, "e")': (i32, &'static str) 137..138 '1': i32 - 140..143 '"e"': &str - 154..155 'f': ((i32, &str), &str) - 158..166 '(e, "d")': ((i32, &str), &str) - 159..160 'e': (i32, &str) - 162..165 '"d"': &str + 140..143 '"e"': &'static str + 154..155 'f': ((i32, &'static str), &'static str) + 158..166 '(e, "d")': ((i32, &'static str), &'static str) + 159..160 'e': (i32, &'static str) + 162..165 '"d"': &'static str "#]], ); } @@ -1156,20 +1156,20 @@ fn infer_array() { } "#, expect![[r#" - 8..9 'x': &str + 8..9 'x': &'? str 17..18 'y': isize 27..326 '{ ...,4]; }': () - 37..38 'a': [&str; 1] - 41..44 '[x]': [&str; 1] - 42..43 'x': &str - 54..55 'b': [[&str; 1]; 2] - 58..64 '[a, a]': [[&str; 1]; 2] - 59..60 'a': [&str; 1] - 62..63 'a': [&str; 1] - 74..75 'c': [[[&str; 1]; 2]; 2] - 78..84 '[b, b]': [[[&str; 1]; 2]; 2] - 79..80 'b': [[&str; 1]; 2] - 82..83 'b': [[&str; 1]; 2] + 37..38 'a': [&'? str; 1] + 41..44 '[x]': [&'? str; 1] + 42..43 'x': &'? str + 54..55 'b': [[&'? str; 1]; 2] + 58..64 '[a, a]': [[&'? str; 1]; 2] + 59..60 'a': [&'? str; 1] + 62..63 'a': [&'? str; 1] + 74..75 'c': [[[&'? str; 1]; 2]; 2] + 78..84 '[b, b]': [[[&'? str; 1]; 2]; 2] + 79..80 'b': [[&'? str; 1]; 2] + 82..83 'b': [[&'? str; 1]; 2] 95..96 'd': [isize; 4] 99..111 '[y, 1, 2, 3]': [isize; 4] 100..101 'y': isize @@ -1197,15 +1197,15 @@ fn infer_array() { 209..215 '[1, 2]': [i32; 2] 210..211 '1': i32 213..214 '2': i32 - 225..226 'i': [&str; 2] - 229..239 '["a", "b"]': [&str; 2] - 230..233 '"a"': &str - 235..238 '"b"': &str - 250..251 'b': [[&str; 1]; 2] - 254..264 '[a, ["b"]]': [[&str; 1]; 2] - 255..256 'a': [&str; 1] - 258..263 '["b"]': [&str; 1] - 259..262 '"b"': &str + 225..226 'i': [&'? str; 2] + 229..239 '["a", "b"]': [&'? str; 2] + 230..233 '"a"': &'static str + 235..238 '"b"': &'static str + 250..251 'b': [[&'? str; 1]; 2] + 254..264 '[a, ["b"]]': [[&'? str; 1]; 2] + 255..256 'a': [&'? str; 1] + 258..263 '["b"]': [&'? str; 1] + 259..262 '"b"': &'static str 274..275 'x': [u8; 0] 287..289 '[]': [u8; 0] 299..300 'y': [u8; 4] @@ -1279,12 +1279,12 @@ fn infer_tuple_struct_generics() { 92..93 'A': extern "rust-call" A(u128) -> A 92..101 'A(42u128)': A 94..100 '42u128': u128 - 107..111 'Some': extern "rust-call" Some<&str>(&str) -> Option<&str> - 107..116 'Some("x")': Option<&str> - 112..115 '"x"': &str - 122..134 'Option::Some': extern "rust-call" Some<&str>(&str) -> Option<&str> - 122..139 'Option...e("x")': Option<&str> - 135..138 '"x"': &str + 107..111 'Some': extern "rust-call" Some<&'static str>(&'static str) -> Option<&'static str> + 107..116 'Some("x")': Option<&'static str> + 112..115 '"x"': &'static str + 122..134 'Option::Some': extern "rust-call" Some<&'static str>(&'static str) -> Option<&'static str> + 122..139 'Option...e("x")': Option<&'static str> + 135..138 '"x"': &'static str 145..149 'None': Option<{unknown}> 159..160 'x': Option 176..180 'None': Option @@ -1405,15 +1405,15 @@ fn infer_impl_generics_with_autoderef() { } "#, expect![[r#" - 77..81 'self': &Option - 97..99 '{}': Option<&T> + 77..81 'self': &'? Option + 97..99 '{}': Option<&'? T> 110..111 'o': Option 126..164 '{ ...f(); }': () - 132..145 '(&o).as_ref()': Option<&u32> - 133..135 '&o': &Option + 132..145 '(&o).as_ref()': Option<&'? u32> + 133..135 '&o': &'? Option 134..135 'o': Option 151..152 'o': Option - 151..161 'o.as_ref()': Option<&u32> + 151..161 'o.as_ref()': Option<&'? u32> "#]], ); } @@ -1551,16 +1551,16 @@ fn infer_type_alias() { "#, expect![[r#" 115..116 'x': A - 123..124 'y': A<&str, u128> + 123..124 'y': A<&'? str, u128> 137..138 'z': A 153..210 '{ ...z.y; }': () 159..160 'x': A 159..162 'x.x': u32 168..169 'x': A 168..171 'x.y': i128 - 177..178 'y': A<&str, u128> - 177..180 'y.x': &str - 186..187 'y': A<&str, u128> + 177..178 'y': A<&'? str, u128> + 177..180 'y.x': &'? str + 186..187 'y': A<&'? str, u128> 186..189 'y.y': u128 195..196 'z': A 195..198 'z.x': u8 @@ -1572,8 +1572,8 @@ fn infer_type_alias() { 312..328 'm::Ali...Foo(0)': Enum 326..327 '0': u8 338..354 'm::Ali...Foo(x)': Enum - 352..353 'x': &u8 - 357..359 '&e': &Enum + 352..353 'x': &'? u8 + 357..359 '&e': &'? Enum 358..359 'e': Enum "#]], ) @@ -1618,10 +1618,10 @@ fn infer_type_param() { 9..10 'x': T 20..29 '{ x }': T 26..27 'x': T - 43..44 'x': &T + 43..44 'x': &'? T 55..65 '{ *x }': T 61..63 '*x': T - 62..63 'x': &T + 62..63 'x': &'? T 77..157 '{ ...(1); }': () 87..88 'y': u32 91..96 '10u32': u32 @@ -1629,9 +1629,9 @@ fn infer_type_param() { 102..107 'id(y)': u32 105..106 'y': u32 117..118 'x': bool - 127..132 'clone': fn clone(&bool) -> bool + 127..132 'clone': fn clone(&'? bool) -> bool 127..135 'clone(z)': bool - 133..134 'z': &bool + 133..134 'z': &'? bool 141..151 'id::': fn id(i128) -> i128 141..154 'id::(1)': i128 152..153 '1': i128 @@ -1842,7 +1842,7 @@ fn foo() -> &'static str { "" } fn main() { foo(); - //^^^^^ &str + //^^^^^ &'static str }"#, ); } @@ -1940,10 +1940,10 @@ fn closure_return_inferred() { "#, expect![[r#" 16..46 '{ ..." }; }': u32 - 26..27 'x': impl Fn() -> &str - 30..43 '|| { "test" }': impl Fn() -> &str - 33..43 '{ "test" }': &str - 35..41 '"test"': &str + 26..27 'x': impl Fn() -> &'static str + 30..43 '|| { "test" }': impl Fn() -> &'static str + 33..43 '{ "test" }': &'static str + 35..41 '"test"': &'static str "#]], ); } @@ -1975,10 +1975,10 @@ fn test() { 70..71 'v': i64 78..80 '{}': () 91..362 '{ ... } }': () - 101..106 'mut g': |usize| yields i64 -> &str - 109..218 '|r| { ... }': |usize| yields i64 -> &str + 101..106 'mut g': |usize| yields i64 -> &'static str + 109..218 '|r| { ... }': |usize| yields i64 -> &'static str 110..111 'r': usize - 113..218 '{ ... }': &str + 113..218 '{ ... }': &'static str 127..128 'a': usize 131..138 'yield 0': usize 137..138 '0': i64 @@ -1988,22 +1988,22 @@ fn test() { 177..178 'a': usize 181..188 'yield 2': usize 187..188 '2': i64 - 198..212 '"return value"': &str + 198..212 '"return value"': &'static str 225..360 'match ... }': () - 231..239 'Pin::new': fn new<&mut |usize| yields i64 -> &str>(&mut |usize| yields i64 -> &str) -> Pin<&mut |usize| yields i64 -> &str> - 231..247 'Pin::n...mut g)': Pin<&mut |usize| yields i64 -> &str> - 231..262 'Pin::n...usize)': CoroutineState - 240..246 '&mut g': &mut |usize| yields i64 -> &str - 245..246 'g': |usize| yields i64 -> &str + 231..239 'Pin::new': fn new<&'? mut |usize| yields i64 -> &'static str>(&'? mut |usize| yields i64 -> &'static str) -> Pin<&'? mut |usize| yields i64 -> &'static str> + 231..247 'Pin::n...mut g)': Pin<&'? mut |usize| yields i64 -> &'static str> + 231..262 'Pin::n...usize)': CoroutineState + 240..246 '&mut g': &'? mut |usize| yields i64 -> &'static str + 245..246 'g': |usize| yields i64 -> &'static str 255..261 '0usize': usize - 273..299 'Corout...ded(y)': CoroutineState + 273..299 'Corout...ded(y)': CoroutineState 297..298 'y': i64 303..312 '{ f(y); }': () 305..306 'f': fn f(i64) 305..309 'f(y)': () 307..308 'y': i64 - 321..348 'Corout...ete(r)': CoroutineState - 346..347 'r': &str + 321..348 'Corout...ete(r)': CoroutineState + 346..347 'r': &'static str 352..354 '{}': () "#]], ); @@ -2050,7 +2050,7 @@ fn f(x: (&&&&i32, &&&i32)) { _ => loop {}, }; f; - //^ (&&&&i32, &&&i32) + //^ (&'? &'? &'? &'? i32, &'? &'? &'? i32) } "#, ); @@ -2059,10 +2059,10 @@ fn f(x: (&&&&i32, &&&i32)) { fn f() { let x = &&&(&&&2, &&&&&3); let (y, z) = x; - //^ &&&&i32 + //^ &'? &'? &'? &'? i32 let t @ (y, z) = x; t; - //^ &&&(&&&i32, &&&&&i32) + //^ &'? &'? &'? (&'? &'? &'? i32, &'? &'? &'? &'? &'? i32) } "#, ); @@ -2071,10 +2071,10 @@ fn f() { fn f() { let x = &&&(&&&2, &&&&&3); let (y, z) = x; - //^ &&&&i32 + //^ &'? &'? &'? &'? i32 let t @ (y, z) = x; t; - //^ &&&(&&&i32, &&&&&i32) + //^ &'? &'? &'? (&'? &'? &'? i32, &'? &'? &'? &'? &'? i32) } "#, ); @@ -2761,23 +2761,23 @@ impl S { fn f() { let x = S; let c1 = || x.read(); - //^^ impl Fn() -> &S + //^^ impl Fn() -> &'? S let c2 = || x.write(); - //^^ impl FnMut() -> &mut S + //^^ impl FnMut() -> &'? mut S let c3 = || x.consume(); //^^ impl FnOnce() -> S let c3 = || x.consume().consume().consume(); //^^ impl FnOnce() -> S let c3 = || x.consume().write().read(); - //^^ impl FnOnce() -> &S + //^^ impl FnOnce() -> &'? S let x = &mut x; let c1 = || x.write(); - //^^ impl FnMut() -> &mut S + //^^ impl FnMut() -> &'? mut S let x = S; let c1 = || { let ref t = x; t }; - //^^ impl Fn() -> &S + //^^ impl Fn() -> &'? S let c2 = || { let ref mut t = x; t }; - //^^ impl FnMut() -> &mut S + //^^ impl FnMut() -> &'? mut S let c3 = || { let t = x; t }; //^^ impl FnOnce() -> S } @@ -3074,11 +3074,11 @@ fn main() { } "#, expect![[r#" - 104..108 'self': &Box - 188..192 'self': &Box> - 218..220 '{}': &T - 242..246 'self': &Box> - 275..277 '{}': &Foo + 104..108 'self': &'? Box + 188..192 'self': &'a Box> + 218..220 '{}': &'a T + 242..246 'self': &'a Box> + 275..277 '{}': &'a Foo 297..301 'self': Box> 322..324 '{}': Foo 338..559 '{ ...r(); }': () @@ -3088,21 +3088,21 @@ fn main() { 360..363 'Foo': extern "rust-call" Foo(i32) -> Foo 360..370 'Foo(0_i32)': Foo 364..369 '0_i32': i32 - 382..386 'bad1': &i32 + 382..386 'bad1': &'? i32 389..394 'boxed': Box> - 389..406 'boxed....nner()': &i32 - 416..421 'good1': &i32 - 424..438 'Foo::get_inner': fn get_inner(&Box>) -> &i32 - 424..446 'Foo::g...boxed)': &i32 - 439..445 '&boxed': &Box> + 389..406 'boxed....nner()': &'? i32 + 416..421 'good1': &'? i32 + 424..438 'Foo::get_inner': fn get_inner(&'? Box>) -> &'? i32 + 424..446 'Foo::g...boxed)': &'? i32 + 439..445 '&boxed': &'? Box> 440..445 'boxed': Box> - 457..461 'bad2': &Foo + 457..461 'bad2': &'? Foo 464..469 'boxed': Box> - 464..480 'boxed....self()': &Foo - 490..495 'good2': &Foo - 498..511 'Foo::get_self': fn get_self(&Box>) -> &Foo - 498..519 'Foo::g...boxed)': &Foo - 512..518 '&boxed': &Box> + 464..480 'boxed....self()': &'? Foo + 490..495 'good2': &'? Foo + 498..511 'Foo::get_self': fn get_self(&'? Box>) -> &'? Foo + 498..519 'Foo::g...boxed)': &'? Foo + 512..518 '&boxed': &'? Box> 513..518 'boxed': Box> 530..535 'inner': Foo 538..543 'boxed': Box> @@ -3414,31 +3414,31 @@ struct TS(usize); fn main() { let x; [x,] = &[1,]; - //^^^^expected &[i32; 1], got [{unknown}; _] + //^^^^expected &'? [i32; 1], got [{unknown}; _] let x; [(x,),] = &[(1,),]; - //^^^^^^^expected &[(i32,); 1], got [{unknown}; _] + //^^^^^^^expected &'? [(i32,); 1], got [{unknown}; _] let x; ((x,),) = &((1,),); - //^^^^^^^expected &((i32,),), got (({unknown},),) + //^^^^^^^expected &'? ((i32,),), got (({unknown},),) let x; (x,) = &(1,); - //^^^^expected &(i32,), got ({unknown},) + //^^^^expected &'? (i32,), got ({unknown},) let x; (S { a: x },) = &(S { a: 42 },); - //^^^^^^^^^^^^^expected &(S,), got (S,) + //^^^^^^^^^^^^^expected &'? (S,), got (S,) let x; S { a: x } = &S { a: 42 }; - //^^^^^^^^^^expected &S, got S + //^^^^^^^^^^expected &'? S, got S let x; TS(x) = &TS(42); - //^^^^^expected &TS, got TS + //^^^^^expected &'? TS, got TS } "#, ); @@ -3548,17 +3548,17 @@ fn f(t: Ark) { } "#, expect![[r#" - 47..51 'self': &Ark + 47..51 'self': &'? Ark 65..88 '{ ... }': *const T - 75..82 '&self.0': &T - 76..80 'self': &Ark + 75..82 '&self.0': &'? T + 76..80 'self': &'? Ark 76..82 'self.0': T 99..100 't': Ark 110..144 '{ ... (); }': () - 116..124 'Ark::foo': fn foo(&Ark) -> *const T + 116..124 'Ark::foo': fn foo(&'? Ark) -> *const T 116..128 'Ark::foo(&t)': *const T 116..141 'Ark::f...nst ()': *const () - 125..127 '&t': &Ark + 125..127 '&t': &'? Ark 126..127 't': Ark "#]], ); @@ -3632,7 +3632,7 @@ pub struct CStr; fn main() { c"ello"; - //^^^^^^^ &CStr + //^^^^^^^ &'static CStr } "#, ); @@ -3659,7 +3659,25 @@ fn main() { let are = "are"; let count = 10; builtin#format_args("hello {count:02} {} friends, we {are:?} {0}{last}", "fancy", last = "!"); - // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type: Arguments<'{error}> + // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type: Arguments<'?> +} +"#, + ); +} + +#[test] +fn inline_const_expression() { + check( + r#" +fn main() { + let foo = 0; + const { + let bar = 1; + let unresolved = foo; + // ^^^^^^^^^^ type: {unknown} + let resolved = bar; + // ^^^^^^^^ type: i32 + } } "#, ); diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/traits.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/traits.rs index dfcd322a39c73..eb2c60950129b 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/traits.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/traits.rs @@ -245,7 +245,7 @@ fn test() { v.push("foo"); for x in v { x; - } //^ &str + } //^ &'static str } //- /alloc.rs crate:alloc @@ -575,7 +575,7 @@ fn indexing_arrays() { "fn main() { &mut [9][2]; }", expect![[r#" 10..26 '{ &mut...[2]; }': () - 12..23 '&mut [9][2]': &mut {unknown} + 12..23 '&mut [9][2]': &'? mut {unknown} 17..20 '[9]': [i32; 1] 17..23 '[9][2]': {unknown} 18..19 '9': i32 @@ -873,7 +873,7 @@ impl> O { fn test(o: O) { o.foo(); -} //^^^^^^^ &str +} //^^^^^^^ &'? str "#, ); } @@ -1016,15 +1016,15 @@ fn test(x: impl Trait, y: &impl Trait) { z.foo2(); }"#, expect![[r#" - 29..33 'self': &Self - 54..58 'self': &Self + 29..33 'self': &'? Self + 54..58 'self': &'? Self 77..78 'x': impl Trait 97..99 '{}': () 154..155 'x': impl Trait - 174..175 'y': &impl Trait + 174..175 'y': &'? impl Trait 195..323 '{ ...2(); }': () 201..202 'x': impl Trait - 208..209 'y': &impl Trait + 208..209 'y': &'? impl Trait 219..220 'z': S 223..224 'S': extern "rust-call" S(u16) -> S 223..227 'S(1)': S @@ -1034,13 +1034,13 @@ fn test(x: impl Trait, y: &impl Trait) { 237..238 'z': S 245..246 'x': impl Trait 245..252 'x.foo()': u64 - 258..259 'y': &impl Trait + 258..259 'y': &'? impl Trait 258..265 'y.foo()': u32 271..272 'z': S 271..278 'z.foo()': u16 284..285 'x': impl Trait 284..292 'x.foo2()': i64 - 298..299 'y': &impl Trait + 298..299 'y': &'? impl Trait 298..306 'y.foo2()': i64 312..313 'z': S 312..320 'z.foo2()': i64 @@ -1204,26 +1204,26 @@ fn test(x: impl Trait, y: &impl Trait) { z.foo2(); }"#, expect![[r#" - 29..33 'self': &Self - 54..58 'self': &Self + 29..33 'self': &'? Self + 54..58 'self': &'? Self 98..100 '{}': () 110..111 'x': impl Trait - 130..131 'y': &impl Trait + 130..131 'y': &'? impl Trait 151..268 '{ ...2(); }': () 157..158 'x': impl Trait - 164..165 'y': &impl Trait + 164..165 'y': &'? impl Trait 175..176 'z': impl Trait 179..182 'bar': fn bar() -> impl Trait 179..184 'bar()': impl Trait 190..191 'x': impl Trait 190..197 'x.foo()': u64 - 203..204 'y': &impl Trait + 203..204 'y': &'? impl Trait 203..210 'y.foo()': u64 216..217 'z': impl Trait 216..223 'z.foo()': u64 229..230 'x': impl Trait 229..237 'x.foo2()': i64 - 243..244 'y': &impl Trait + 243..244 'y': &'? impl Trait 243..251 'y.foo2()': i64 257..258 'z': impl Trait 257..265 'z.foo2()': i64 @@ -1328,7 +1328,7 @@ fn test() { a.foo(); }"#, expect![[r#" - 29..33 'self': &Self + 29..33 'self': &'? Self 71..82 '{ loop {} }': ! 73..80 'loop {}': ! 78..80 '{}': () @@ -1366,8 +1366,8 @@ fn test() { d.foo(); }"#, expect![[r#" - 49..53 'self': &mut Self - 101..105 'self': &Self + 49..53 'self': &'? mut Self + 101..105 'self': &'? Self 184..195 '{ loop {} }': ({unknown}, {unknown}) 186..193 'loop {}': ! 191..193 '{}': () @@ -1414,10 +1414,10 @@ fn foo() -> (impl FnOnce(&str, T), impl Trait) { } "#, expect![[r#" - 134..165 '{ ...(C)) }': (impl FnOnce(&str, T), Bar) - 140..163 '(|inpu...ar(C))': (impl FnOnce(&str, T), Bar) - 141..154 '|input, t| {}': impl FnOnce(&str, T) - 142..147 'input': &str + 134..165 '{ ...(C)) }': (impl FnOnce(&'? str, T), Bar) + 140..163 '(|inpu...ar(C))': (impl FnOnce(&'? str, T), Bar) + 141..154 '|input, t| {}': impl FnOnce(&'? str, T) + 142..147 'input': &'? str 149..150 't': T 152..154 '{}': () 156..159 'Bar': extern "rust-call" Bar(u8) -> Bar @@ -1466,26 +1466,26 @@ fn test(x: dyn Trait, y: &dyn Trait) { z.foo2(); }"#, expect![[r#" - 29..33 'self': &Self - 54..58 'self': &Self + 29..33 'self': &'? Self + 54..58 'self': &'? Self 97..99 '{}': dyn Trait 109..110 'x': dyn Trait - 128..129 'y': &dyn Trait + 128..129 'y': &'? dyn Trait 148..265 '{ ...2(); }': () 154..155 'x': dyn Trait - 161..162 'y': &dyn Trait + 161..162 'y': &'? dyn Trait 172..173 'z': dyn Trait 176..179 'bar': fn bar() -> dyn Trait 176..181 'bar()': dyn Trait 187..188 'x': dyn Trait 187..194 'x.foo()': u64 - 200..201 'y': &dyn Trait + 200..201 'y': &'? dyn Trait 200..207 'y.foo()': u64 213..214 'z': dyn Trait 213..220 'z.foo()': u64 226..227 'x': dyn Trait 226..234 'x.foo2()': i64 - 240..241 'y': &dyn Trait + 240..241 'y': &'? dyn Trait 240..248 'y.foo2()': i64 254..255 'z': dyn Trait 254..262 'z.foo2()': i64 @@ -1514,16 +1514,16 @@ fn test(s: S) { s.bar().baz(); }"#, expect![[r#" - 32..36 'self': &Self - 102..106 'self': &S - 128..139 '{ loop {} }': &dyn Trait + 32..36 'self': &'? Self + 102..106 'self': &'? S + 128..139 '{ loop {} }': &'? dyn Trait 130..137 'loop {}': ! 135..137 '{}': () - 175..179 'self': &Self + 175..179 'self': &'? Self 251..252 's': S 267..289 '{ ...z(); }': () 273..274 's': S - 273..280 's.bar()': &dyn Trait + 273..280 's.bar()': &'? dyn Trait 273..286 's.bar().baz()': (u32, i32) "#]], ); @@ -1548,19 +1548,19 @@ fn test(x: Trait, y: &Trait) -> u64 { z.foo(); }"#, expect![[r#" - 26..30 'self': &Self + 26..30 'self': &'? Self 60..62 '{}': dyn Trait 72..73 'x': dyn Trait - 82..83 'y': &dyn Trait + 82..83 'y': &'? dyn Trait 100..175 '{ ...o(); }': u64 106..107 'x': dyn Trait - 113..114 'y': &dyn Trait + 113..114 'y': &'? dyn Trait 124..125 'z': dyn Trait 128..131 'bar': fn bar() -> dyn Trait 128..133 'bar()': dyn Trait 139..140 'x': dyn Trait 139..146 'x.foo()': u64 - 152..153 'y': &dyn Trait + 152..153 'y': &'? dyn Trait 152..159 'y.foo()': u64 165..166 'z': dyn Trait 165..172 'z.foo()': u64 @@ -1580,14 +1580,14 @@ fn main() { } "#, expect![[r#" - 31..35 'self': &S + 31..35 'self': &'? S 37..39 '{}': () - 47..48 '_': &dyn Fn(S) + 47..48 '_': &'? dyn Fn(S) 58..60 '{}': () 71..105 '{ ...()); }': () - 77..78 'f': fn f(&dyn Fn(S)) + 77..78 'f': fn f(&'? dyn Fn(S)) 77..102 'f(&|nu...foo())': () - 79..101 '&|numb....foo()': &impl Fn(S) + 79..101 '&|numb....foo()': &'? impl Fn(S) 80..101 '|numbe....foo()': impl Fn(S) 81..87 'number': S 89..95 'number': S @@ -1790,7 +1790,7 @@ fn test(x: T, y: U) { y.foo(); }"#, expect![[r#" - 53..57 'self': &Self + 53..57 'self': &'? Self 66..68 '{}': u32 185..186 'x': T 191..192 'y': U @@ -1819,11 +1819,11 @@ fn test(x: &impl Trait1) { x.foo(); }"#, expect![[r#" - 53..57 'self': &Self + 53..57 'self': &'? Self 66..68 '{}': u32 - 119..120 'x': &impl Trait1 + 119..120 'x': &'? impl Trait1 136..152 '{ ...o(); }': () - 142..143 'x': &impl Trait1 + 142..143 'x': &'? impl Trait1 142..149 'x.foo()': u32 "#]], ); @@ -1934,8 +1934,8 @@ fn test() { opt.map(f); }"#, expect![[r#" - 28..32 'self': &Self - 132..136 'self': &Bar + 28..32 'self': &'? Self + 132..136 'self': &'? Bar 149..160 '{ loop {} }': (A1, R) 151..158 'loop {}': ! 156..158 '{}': () @@ -1988,7 +1988,7 @@ fn test() { let r2 = lazy2.foo(); }"#, expect![[r#" - 36..40 'self': &Foo + 36..40 'self': &'? Foo 51..53 '{}': usize 131..132 'f': F 151..153 '{}': Lazy @@ -2262,14 +2262,14 @@ impl Trait for S2 { fn f(&self, x: ::Item) { let y = x; } }"#, expect![[r#" - 40..44 'self': &Self + 40..44 'self': &'? Self 46..47 'x': Trait::Item - 126..130 'self': &S + 126..130 'self': &'? S 132..133 'x': u32 147..161 '{ let y = x; }': () 153..154 'y': u32 157..158 'x': u32 - 228..232 'self': &S2 + 228..232 'self': &'? S2 234..235 'x': i32 251..265 '{ let y = x; }': () 257..258 'y': i32 @@ -2643,12 +2643,12 @@ fn main() { 72..74 '_v': F 117..120 '{ }': () 132..163 '{ ... }); }': () - 138..148 'f::<(), _>': fn f<(), impl FnOnce(&())>(impl FnOnce(&())) + 138..148 'f::<(), _>': fn f<(), impl FnOnce(&'? ())>(impl FnOnce(&'? ())) 138..160 'f::<()... z; })': () - 149..159 '|z| { z; }': impl FnOnce(&()) - 150..151 'z': &() + 149..159 '|z| { z; }': impl FnOnce(&'? ()) + 150..151 'z': &'? () 153..159 '{ z; }': () - 155..156 'z': &() + 155..156 'z': &'? () "#]], ); } @@ -2897,13 +2897,13 @@ fn test(x: &dyn Foo) { foo(x); }"#, expect![[r#" - 21..22 'x': &dyn Foo + 21..22 'x': &'? dyn Foo 34..36 '{}': () - 46..47 'x': &dyn Foo + 46..47 'x': &'? dyn Foo 59..74 '{ foo(x); }': () - 65..68 'foo': fn foo(&dyn Foo) + 65..68 'foo': fn foo(&'? dyn Foo) 65..71 'foo(x)': () - 69..70 'x': &dyn Foo + 69..70 'x': &'? dyn Foo "#]], ); } @@ -2927,7 +2927,7 @@ fn test() { (IsCopy, NotCopy).test(); }"#, expect![[r#" - 78..82 'self': &Self + 78..82 'self': &'? Self 134..235 '{ ...t(); }': () 140..146 'IsCopy': IsCopy 140..153 'IsCopy.test()': bool @@ -2969,7 +2969,7 @@ fn test() { 28..29 'T': {unknown} 36..38 '{}': T 36..38: expected T, got () - 113..117 'self': &Self + 113..117 'self': &'? Self 169..249 '{ ...t(); }': () 175..178 'foo': fn foo() 175..185 'foo.test()': bool @@ -2997,16 +2997,16 @@ fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) { f3.test(); }"#, expect![[r#" - 22..26 'self': &Self + 22..26 'self': &'? Self 76..78 'f1': fn() 86..88 'f2': fn(usize) -> u8 - 107..109 'f3': fn(u8, u8) -> &u8 + 107..109 'f3': fn(u8, u8) -> &'? u8 130..178 '{ ...t(); }': () 136..138 'f1': fn() 136..145 'f1.test()': bool 151..153 'f2': fn(usize) -> u8 151..160 'f2.test()': bool - 166..168 'f3': fn(u8, u8) -> &u8 + 166..168 'f3': fn(u8, u8) -> &'? u8 166..175 'f3.test()': bool "#]], ); @@ -3027,13 +3027,13 @@ fn test() { (1u8, *"foo").test(); // not Sized }"#, expect![[r#" - 22..26 'self': &Self + 22..26 'self': &'? Self 79..194 '{ ...ized }': () 85..88 '1u8': u8 85..95 '1u8.test()': bool 101..116 '(*"foo").test()': {unknown} 102..108 '*"foo"': str - 103..108 '"foo"': &str + 103..108 '"foo"': &'static str 135..145 '(1u8, 1u8)': (u8, u8) 135..152 '(1u8, ...test()': bool 136..139 '1u8': u8 @@ -3042,7 +3042,7 @@ fn test() { 158..178 '(1u8, ...test()': {unknown} 159..162 '1u8': u8 164..170 '*"foo"': str - 165..170 '"foo"': &str + 165..170 '"foo"': &'static str "#]], ); } @@ -3093,7 +3093,7 @@ fn foo() { 93..94 'x': Option 109..111 '{}': () 117..124 '(&f)(s)': () - 118..120 '&f': &impl Fn(Option) + 118..120 '&f': &'? impl Fn(Option) 119..120 'f': impl Fn(Option) 122..123 's': Option "#]], @@ -3170,25 +3170,25 @@ fn foo() { f(&s); }"#, expect![[r#" - 154..158 'self': &Box - 166..205 '{ ... }': &T - 176..199 'unsafe...nner }': &T - 185..197 '&*self.inner': &T + 154..158 'self': &'? Box + 166..205 '{ ... }': &'? T + 176..199 'unsafe...nner }': &'? T + 185..197 '&*self.inner': &'? T 186..197 '*self.inner': T - 187..191 'self': &Box + 187..191 'self': &'? Box 187..197 'self.inner': *mut T 218..324 '{ ...&s); }': () 228..229 's': Option 232..236 'None': Option - 246..247 'f': Box)> - 281..310 'Box { ... {}) }': Box)> - 294..308 '&mut (|ps| {})': &mut impl FnOnce(&Option) - 300..307 '|ps| {}': impl FnOnce(&Option) - 301..303 'ps': &Option + 246..247 'f': Box)> + 281..310 'Box { ... {}) }': Box)> + 294..308 '&mut (|ps| {})': &'? mut impl FnOnce(&'? Option) + 300..307 '|ps| {}': impl FnOnce(&'? Option) + 301..303 'ps': &'? Option 305..307 '{}': () - 316..317 'f': Box)> + 316..317 'f': Box)> 316..321 'f(&s)': () - 318..320 '&s': &Option + 318..320 '&s': &'? Option 319..320 's': Option "#]], ); @@ -3320,7 +3320,7 @@ fn f() { } }"#, expect![[r#" - 46..50 'self': &Self + 46..50 'self': &'? Self 58..63 '{ 0 }': u8 60..61 '0': u8 115..185 '{ ... } }': () @@ -3595,7 +3595,7 @@ fn take_u32(_: u32) {} fn minimized() { let v = V::default(); let p = v.get(&0); - //^ &u32 + //^ &'? u32 take_u32(42 + p); } "#, @@ -3625,7 +3625,7 @@ fn take_u32(_: u32) {} fn minimized() { let v = V::default(); let p = v.get(); - //^ &{unknown} + //^ &'? {unknown} take_u32(42 + p); } "#, @@ -3684,11 +3684,11 @@ fn main() { } "#, expect![[r#" - 44..48 'self': &Self - 133..137 'self': &[u8; 4] + 44..48 'self': &'? Self + 133..137 'self': &'? [u8; 4] 155..172 '{ ... }': usize 165..166 '2': usize - 236..240 'self': &[u8; 2] + 236..240 'self': &'? [u8; 2] 258..275 '{ ... }': u8 268..269 '2': u8 289..392 '{ ...g(); }': () @@ -3732,11 +3732,11 @@ fn main() { } "#, expect![[r#" - 44..48 'self': &Self - 151..155 'self': &[u8; L] + 44..48 'self': &'? Self + 151..155 'self': &'? [u8; L] 173..194 '{ ... }': [u8; L] 183..188 '*self': [u8; L] - 184..188 'self': &[u8; L] + 184..188 'self': &'? [u8; L] 208..260 '{ ...g(); }': () 218..219 'v': [u8; 2] 222..230 '[0u8; 2]': [u8; 2] @@ -4056,13 +4056,13 @@ fn g(t: &(dyn Sync + T2 + T1 + Send)) { } "#, expect![[r#" - 68..69 't': &{unknown} + 68..69 't': &'? {unknown} 101..103 '{}': () - 109..110 't': &{unknown} + 109..110 't': &'? {unknown} 142..155 '{ f(t); }': () - 148..149 'f': fn f(&{unknown}) + 148..149 'f': fn f(&'? {unknown}) 148..152 'f(t)': () - 150..151 't': &{unknown} + 150..151 't': &'? {unknown} "#]], ); @@ -4105,7 +4105,7 @@ trait Trait { } fn f(t: &dyn Trait) {} - //^&{unknown} + //^&'? {unknown} "#, ); } @@ -4175,27 +4175,27 @@ trait Trait { fn f(v: impl Trait) { let a = v.get::().deref(); - //^ &i32 + //^ &'? i32 let a = v.get::().deref(); - //^ &T + //^ &'? T } fn g<'a, T: 'a>(v: impl Trait = &'a T>) { let a = v.get::(); - //^ &T + //^ &'a T let a = v.get::<()>(); - //^ Trait::Assoc<(), impl Trait = &T>> + //^ Trait::Assoc<(), impl Trait = &'a T>> } fn h<'a>(v: impl Trait = &'a i32> + Trait = &'a i64>) { let a = v.get::(); - //^ &i32 + //^ &'a i32 let a = v.get::(); - //^ &i64 + //^ &'a i64 } fn i<'a>(v: impl Trait = &'a i32, Assoc = &'a i64>) { let a = v.get::(); - //^ &i32 + //^ &'a i32 let a = v.get::(); - //^ &i64 + //^ &'a i64 } "#, ); @@ -4221,12 +4221,12 @@ fn f<'a>(v: &dyn Trait = &'a i32>) { } "#, expect![[r#" - 90..94 'self': &Self - 127..128 'v': &(dyn Trait = &i32>) + 90..94 'self': &'? Self + 127..128 'v': &'? (dyn Trait = &'a i32>) 164..195 '{ ...f(); }': () - 170..171 'v': &(dyn Trait = &i32>) - 170..184 'v.get::()': &i32 - 170..192 'v.get:...eref()': &i32 + 170..171 'v': &'? (dyn Trait = &'a i32>) + 170..184 'v.get::()': &'? i32 + 170..192 'v.get:...eref()': &'? i32 "#]], ); } @@ -4487,19 +4487,19 @@ fn derive_macro_bounds() { let x = (&Copy).clone(); //^ Copy let x = (&NotCopy).clone(); - //^ &NotCopy + //^ &'? NotCopy let x = (&Generic(Copy)).clone(); //^ Generic let x = (&Generic(NotCopy)).clone(); - //^ &Generic + //^ &'? Generic let x: &AssocGeneric = &AssocGeneric(NotCopy); let x = x.clone(); - //^ &AssocGeneric + //^ &'? AssocGeneric // let x: &AssocGeneric2 = &AssocGeneric2(NotCopy); // let x = x.clone(); let x: &AssocGeneric3 = &AssocGeneric3(Generic(NotCopy)); let x = x.clone(); - //^ &AssocGeneric3 + //^ &'? AssocGeneric3 let x = (&R1(Vec())).clone(); //^ R1 let x = (&R2(R1(Vec()))).clone(); @@ -4582,7 +4582,7 @@ impl B for u16 { fn ttt() { let inp = Y; x::(&inp); - //^^^^ expected &X, got &Y + //^^^^ expected &'? X, got &'? Y } "#, ); @@ -4629,7 +4629,7 @@ fn foo() { let mut map = SomeMap; map["a"] = (); map; - //^^^ SomeMap<&str> + //^^^ SomeMap<&'static str> } "#, ); diff --git a/src/tools/rust-analyzer/crates/hir/src/diagnostics.rs b/src/tools/rust-analyzer/crates/hir/src/diagnostics.rs index 4518422d27e95..6e3d366c88b86 100644 --- a/src/tools/rust-analyzer/crates/hir/src/diagnostics.rs +++ b/src/tools/rust-analyzer/crates/hir/src/diagnostics.rs @@ -9,6 +9,7 @@ use hir_ty::{db::HirDatabase, diagnostics::BodyValidationDiagnostic, InferenceDi use base_db::CrateId; use cfg::{CfgExpr, CfgOptions}; use either::Either; +pub use hir_def::VariantId; use hir_def::{body::SyntheticSyntax, hir::ExprOrPatId, path::ModPath, AssocItemId, DefWithBodyId}; use hir_expand::{name::Name, HirFileId, InFile}; use syntax::{ast, AstPtr, SyntaxError, SyntaxNodePtr, TextRange}; @@ -200,6 +201,7 @@ pub struct MalformedDerive { pub struct NoSuchField { pub field: InFile>>, pub private: bool, + pub variant: VariantId, } #[derive(Debug)] @@ -525,7 +527,7 @@ impl AnyDiagnostic { source_map.pat_syntax(pat).inspect_err(|_| tracing::error!("synthetic syntax")).ok() }; Some(match d { - &InferenceDiagnostic::NoSuchField { field: expr, private } => { + &InferenceDiagnostic::NoSuchField { field: expr, private, variant } => { let expr_or_pat = match expr { ExprOrPatId::ExprId(expr) => { source_map.field_syntax(expr).map(AstPtr::wrap_left) @@ -534,7 +536,7 @@ impl AnyDiagnostic { source_map.pat_field_syntax(pat).map(AstPtr::wrap_right) } }; - NoSuchField { field: expr_or_pat, private }.into() + NoSuchField { field: expr_or_pat, private, variant }.into() } &InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => { MismatchedArgCount { call_expr: expr_syntax(call_expr)?, expected, found }.into() diff --git a/src/tools/rust-analyzer/crates/hir/src/display.rs b/src/tools/rust-analyzer/crates/hir/src/display.rs index 84f03d111f275..c276e87786dde 100644 --- a/src/tools/rust-analyzer/crates/hir/src/display.rs +++ b/src/tools/rust-analyzer/crates/hir/src/display.rs @@ -188,28 +188,7 @@ impl HirDisplay for Struct { StructKind::Record => { let has_where_clause = write_where_clause(def_id, f)?; if let Some(limit) = f.entity_limit { - let fields = self.fields(f.db); - let count = fields.len().min(limit); - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; - if count == 0 { - if fields.is_empty() { - f.write_str("{}")?; - } else { - f.write_str("{ /* … */ }")?; - } - } else { - f.write_str(" {\n")?; - for field in &fields[..count] { - f.write_str(" ")?; - field.hir_fmt(f)?; - f.write_str(",\n")?; - } - - if fields.len() > count { - f.write_str(" /* … */\n")?; - } - f.write_str("}")?; - } + display_fields(&self.fields(f.db), has_where_clause, limit, false, f)?; } } StructKind::Unit => _ = write_where_clause(def_id, f)?, @@ -226,18 +205,10 @@ impl HirDisplay for Enum { write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id)); write_generic_params(def_id, f)?; - let has_where_clause = write_where_clause(def_id, f)?; - let variants = self.variants(f.db); - if !variants.is_empty() { - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; - f.write_str("{\n")?; - for variant in variants { - f.write_str(" ")?; - variant.hir_fmt(f)?; - f.write_str(",\n")?; - } - f.write_str("}")?; + let has_where_clause = write_where_clause(def_id, f)?; + if let Some(limit) = f.entity_limit { + display_variants(&self.variants(f.db), has_where_clause, limit, f)?; } Ok(()) @@ -251,22 +222,102 @@ impl HirDisplay for Union { write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id)); write_generic_params(def_id, f)?; + let has_where_clause = write_where_clause(def_id, f)?; + if let Some(limit) = f.entity_limit { + display_fields(&self.fields(f.db), has_where_clause, limit, false, f)?; + } + Ok(()) + } +} + +fn display_fields( + fields: &[Field], + has_where_clause: bool, + limit: usize, + in_line: bool, + f: &mut HirFormatter<'_>, +) -> Result<(), HirDisplayError> { + let count = fields.len().min(limit); + let (indent, separator) = if in_line { ("", ' ') } else { (" ", '\n') }; + f.write_char(if !has_where_clause { ' ' } else { separator })?; + if count == 0 { + if fields.is_empty() { + f.write_str("{}")?; + } else { + f.write_str("{ /* … */ }")?; + } + } else { + f.write_char('{')?; - let fields = self.fields(f.db); if !fields.is_empty() { - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; - f.write_str("{\n")?; - for field in self.fields(f.db) { - f.write_str(" ")?; + f.write_char(separator)?; + for field in &fields[..count] { + f.write_str(indent)?; field.hir_fmt(f)?; - f.write_str(",\n")?; + f.write_char(',')?; + f.write_char(separator)?; + } + + if fields.len() > count { + f.write_str(indent)?; + f.write_str("/* … */")?; + f.write_char(separator)?; } - f.write_str("}")?; } - Ok(()) + f.write_str("}")?; + } + + Ok(()) +} + +fn display_variants( + variants: &[Variant], + has_where_clause: bool, + limit: usize, + f: &mut HirFormatter<'_>, +) -> Result<(), HirDisplayError> { + let count = variants.len().min(limit); + f.write_char(if !has_where_clause { ' ' } else { '\n' })?; + if count == 0 { + if variants.is_empty() { + f.write_str("{}")?; + } else { + f.write_str("{ /* … */ }")?; + } + } else { + f.write_str("{\n")?; + for variant in &variants[..count] { + f.write_str(" ")?; + write!(f, "{}", variant.name(f.db).display(f.db.upcast()))?; + match variant.kind(f.db) { + StructKind::Tuple => { + if variant.fields(f.db).is_empty() { + f.write_str("()")?; + } else { + f.write_str("( /* … */ )")?; + } + } + StructKind::Record => { + if variant.fields(f.db).is_empty() { + f.write_str(" {}")?; + } else { + f.write_str(" { /* … */ }")?; + } + } + StructKind::Unit => {} + } + f.write_str(",\n")?; + } + + if variants.len() > count { + f.write_str(" /* … */\n")?; + } + f.write_str("}")?; } + + Ok(()) } impl HirDisplay for Field { @@ -304,21 +355,10 @@ impl HirDisplay for Variant { } f.write_char(')')?; } - VariantData::Record(fields) => { - f.write_str(" {")?; - let mut first = true; - for (_, field) in fields.iter() { - if first { - first = false; - f.write_char(' ')?; - } else { - f.write_str(", ")?; - } - // Enum variant fields must be pub. - write!(f, "{}: ", field.name.display(f.db.upcast()))?; - field.type_ref.hir_fmt(f)?; + VariantData::Record(_) => { + if let Some(limit) = f.entity_limit { + display_fields(&self.fields(f.db), false, limit, true, f)?; } - f.write_str(" }")?; } } Ok(()) diff --git a/src/tools/rust-analyzer/crates/hir/src/semantics.rs b/src/tools/rust-analyzer/crates/hir/src/semantics.rs index e792e159acf0a..b99cde6db48e4 100644 --- a/src/tools/rust-analyzer/crates/hir/src/semantics.rs +++ b/src/tools/rust-analyzer/crates/hir/src/semantics.rs @@ -131,7 +131,7 @@ pub struct SemanticsImpl<'db> { pub db: &'db dyn HirDatabase, s2d_cache: RefCell, /// Rootnode to HirFileId cache - cache: RefCell>, + root_to_file_cache: RefCell>, // These 2 caches are mainly useful for semantic highlighting as nothing else descends a lot of tokens // So we might wanna move them out into something specific for semantic highlighting expansion_info_cache: RefCell>, @@ -294,7 +294,7 @@ impl<'db> SemanticsImpl<'db> { SemanticsImpl { db, s2d_cache: Default::default(), - cache: Default::default(), + root_to_file_cache: Default::default(), expansion_info_cache: Default::default(), macro_call_cache: Default::default(), } @@ -690,6 +690,7 @@ impl<'db> SemanticsImpl<'db> { exp_info }); + // FIXME: uncached parse // Create the source analyzer for the macro call scope let Some(sa) = self.analyze_no_infer(&self.parse_or_expand(expansion_info.call_file())) else { @@ -1025,6 +1026,7 @@ impl<'db> SemanticsImpl<'db> { None => { let call_node = file_id.macro_file()?.call_node(db); // cache the node + // FIXME: uncached parse self.parse_or_expand(call_node.file_id); Some(call_node) } @@ -1397,7 +1399,7 @@ impl<'db> SemanticsImpl<'db> { fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { assert!(root_node.parent().is_none()); - let mut cache = self.cache.borrow_mut(); + let mut cache = self.root_to_file_cache.borrow_mut(); let prev = cache.insert(root_node, file_id); assert!(prev.is_none() || prev == Some(file_id)) } @@ -1407,7 +1409,7 @@ impl<'db> SemanticsImpl<'db> { } fn lookup(&self, root_node: &SyntaxNode) -> Option { - let cache = self.cache.borrow(); + let cache = self.root_to_file_cache.borrow(); cache.get(root_node).copied() } @@ -1427,7 +1429,7 @@ impl<'db> SemanticsImpl<'db> { known nodes: {}\n\n", node, root_node, - self.cache + self.root_to_file_cache .borrow() .keys() .map(|it| format!("{it:?}")) diff --git a/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs b/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs index 434e4b5a0cf12..007709a836eda 100644 --- a/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs +++ b/src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs @@ -121,7 +121,7 @@ impl SourceToDefCtx<'_, '_> { let _p = tracing::span!(tracing::Level::INFO, "SourceBinder::file_to_module_def").entered(); let mut mods = SmallVec::new(); for &crate_id in self.db.relevant_crates(file).iter() { - // FIXME: inner items + // Note: `mod` declarations in block modules cannot be supported here let crate_def_map = self.db.crate_def_map(crate_id); mods.extend( crate_def_map @@ -129,6 +129,9 @@ impl SourceToDefCtx<'_, '_> { .map(|local_id| crate_def_map.module_id(local_id)), ) } + if mods.is_empty() { + // FIXME: detached file + } mods } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs index 34326294d2e12..2b8de3443b431 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs @@ -5623,7 +5623,7 @@ fn func(i: Struct<'_, T>) { fun_name(i); } -fn $0fun_name(i: Struct<'_, T>) { +fn $0fun_name(i: Struct) { foo(i); } "#, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs index 66669662316fe..cf135f83e7b6a 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs @@ -77,7 +77,7 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) -> ast::AssocItem::MacroCall(_) => None, }; - name.and_then(|n| ranks.get(&n.to_string()).copied()).unwrap_or(usize::max_value()) + name.and_then(|n| ranks.get(&n.to_string()).copied()).unwrap_or(usize::MAX) }) .collect(); diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs index 64a32dee3d733..62eb642b3bc33 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs @@ -19,7 +19,7 @@ struct Foo<'lt, T, const C: usize> where $0 {} en Enum Enum ma makro!(…) macro_rules! makro md module - st Foo<…> Foo<'{error}, {unknown}, _> + st Foo<…> Foo<{unknown}, _> st Record Record st Tuple Tuple st Unit Unit @@ -92,7 +92,7 @@ struct Foo<'lt, T, const C: usize> where for<'a> $0 {} en Enum Enum ma makro!(…) macro_rules! makro md module - st Foo<…> Foo<'{error}, {unknown}, _> + st Foo<…> Foo<{unknown}, _> st Record Record st Tuple Tuple st Unit Unit diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs index 66f1bff7c1c1a..ff38c16108791 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs @@ -20,8 +20,8 @@ struct Foo<'lt, T, const C: usize> { en Enum Enum ma makro!(…) macro_rules! makro md module - sp Self Foo<'{error}, {unknown}, _> - st Foo<…> Foo<'{error}, {unknown}, _> + sp Self Foo<{unknown}, _> + st Foo<…> Foo<{unknown}, _> st Record Record st Tuple Tuple st Unit Unit @@ -45,8 +45,8 @@ struct Foo<'lt, T, const C: usize>(f$0); en Enum Enum ma makro!(…) macro_rules! makro md module - sp Self Foo<'{error}, {unknown}, _> - st Foo<…> Foo<'{error}, {unknown}, _> + sp Self Foo<{unknown}, _> + st Foo<…> Foo<{unknown}, _> st Record Record st Tuple Tuple st Unit Unit diff --git a/src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs b/src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs index 024e8f6ae39d2..0db87c6bc48f6 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs @@ -1,6 +1,6 @@ //! rust-analyzer is lazy and doesn't compute anything unless asked. This //! sometimes is counter productive when, for example, the first goto definition -//! request takes longer to compute. This modules implemented prepopulation of +//! request takes longer to compute. This module implements prepopulation of //! various caches, it's not really advanced at the moment. mod topologic_sort; diff --git a/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs b/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs index c65467a43249b..12085f9ebd252 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs @@ -301,8 +301,8 @@ impl SymbolIndex { } fn range_to_map_value(start: usize, end: usize) -> u64 { - debug_assert![start <= (std::u32::MAX as usize)]; - debug_assert![end <= (std::u32::MAX as usize)]; + debug_assert![start <= (u32::MAX as usize)]; + debug_assert![end <= (u32::MAX as usize)]; ((start as u64) << 32) | end as u64 } diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs index 241fddbb90635..b3dde977b1c4f 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs @@ -7,7 +7,7 @@ use ide_db::{ helpers::mod_path_to_ast, imports::insert_use::{insert_use, ImportScope}, source_change::SourceChangeBuilder, - RootDatabase, + FxHashMap, RootDatabase, }; use itertools::Itertools; use stdx::{format_to, never}; @@ -22,15 +22,22 @@ use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsConfig, Severity}; #[derive(Default)] struct State { result: String, - struct_counts: usize, has_serialize: bool, has_deserialize: bool, + names: FxHashMap, } impl State { - fn generate_new_name(&mut self) -> ast::Name { - self.struct_counts += 1; - make::name(&format!("Struct{}", self.struct_counts)) + fn generate_new_name(&mut self, name: &str) -> ast::Name { + let name = stdx::to_camel_case(name); + let count = if let Some(count) = self.names.get_mut(&name) { + *count += 1; + *count + } else { + self.names.insert(name.clone(), 1); + 1 + }; + make::name(&format!("{}{}", name, count)) } fn serde_derive(&self) -> String { @@ -52,15 +59,21 @@ impl State { } } - fn build_struct(&mut self, value: &serde_json::Map) -> ast::Type { - let name = self.generate_new_name(); + fn build_struct( + &mut self, + name: &str, + value: &serde_json::Map, + ) -> ast::Type { + let name = self.generate_new_name(name); let ty = make::ty(&name.to_string()); let strukt = make::struct_( None, name, None, make::record_field_list(value.iter().sorted_unstable_by_key(|x| x.0).map( - |(name, value)| make::record_field(None, make::name(name), self.type_of(value)), + |(name, value)| { + make::record_field(None, make::name(name), self.type_of(name, value)) + }, )) .into(), ); @@ -68,7 +81,7 @@ impl State { ty } - fn type_of(&mut self, value: &serde_json::Value) -> ast::Type { + fn type_of(&mut self, name: &str, value: &serde_json::Value) -> ast::Type { match value { serde_json::Value::Null => make::ty_unit(), serde_json::Value::Bool(_) => make::ty("bool"), @@ -76,12 +89,12 @@ impl State { serde_json::Value::String(_) => make::ty("String"), serde_json::Value::Array(it) => { let ty = match it.iter().next() { - Some(x) => self.type_of(x), + Some(x) => self.type_of(name, x), None => make::ty_placeholder(), }; make::ty(&format!("Vec<{ty}>")) } - serde_json::Value::Object(x) => self.build_struct(x), + serde_json::Value::Object(x) => self.build_struct(name, x), } } } @@ -113,7 +126,7 @@ pub(crate) fn json_in_items( let serialize_resolved = scope_resolve("::serde::Serialize"); state.has_deserialize = deserialize_resolved.is_some(); state.has_serialize = serialize_resolved.is_some(); - state.build_struct(&it); + state.build_struct("Root", &it); edit.insert(range.start(), state.result); acc.push( Diagnostic::new( @@ -218,7 +231,7 @@ mod tests { } #[derive(Serialize)] - struct Struct1{ bar: f64, bay: i64, baz: (), r#box: bool, foo: String } + struct Root1{ bar: f64, bay: i64, baz: (), r#box: bool, foo: String } "#, ); @@ -237,9 +250,44 @@ mod tests { } "#, r#" - struct Struct3{ } - struct Struct2{ kind: String, value: Struct3 } - struct Struct1{ bar: Struct2, foo: String } + struct Value1{ } + struct Bar1{ kind: String, value: Value1 } + struct Root1{ bar: Bar1, foo: String } + + "#, + ); + } + + #[test] + fn naming() { + check_fix( + r#" + {$0 + "user": { + "address": { + "street": "Main St", + "house": 3 + }, + "email": "example@example.com" + }, + "another_user": { + "user": { + "address": { + "street": "Main St", + "house": 3 + }, + "email": "example@example.com" + } + } + } + "#, + r#" + struct Address1{ house: i64, street: String } + struct User1{ address: Address1, email: String } + struct AnotherUser1{ user: User1 } + struct Address2{ house: i64, street: String } + struct User2{ address: Address2, email: String } + struct Root1{ another_user: AnotherUser1, user: User2 } "#, ); @@ -276,9 +324,9 @@ mod tests { use serde::Deserialize; #[derive(Serialize, Deserialize)] - struct Struct2{ x: i64, y: i64 } + struct OfObject1{ x: i64, y: i64 } #[derive(Serialize, Deserialize)] - struct Struct1{ empty: Vec<_>, nested: Vec>>, of_object: Vec, of_string: Vec } + struct Root1{ empty: Vec<_>, nested: Vec>>, of_object: Vec, of_string: Vec } "#, ); diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/no_such_field.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/no_such_field.rs index 8d77e566edc9c..5a3206445c547 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/no_such_field.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/no_such_field.rs @@ -1,5 +1,5 @@ use either::Either; -use hir::{db::ExpandDatabase, HasSource, HirDisplay, HirFileIdExt, Semantics}; +use hir::{db::ExpandDatabase, HasSource, HirDisplay, HirFileIdExt, Semantics, VariantId}; use ide_db::{base_db::FileId, source_change::SourceChange, RootDatabase}; use syntax::{ ast::{self, edit::IndentLevel, make}, @@ -25,7 +25,10 @@ pub(crate) fn no_such_field(ctx: &DiagnosticsContext<'_>, d: &hir::NoSuchField) } else { Diagnostic::new_with_syntax_node_ptr( ctx, - DiagnosticCode::RustcHardError("E0559"), + match d.variant { + VariantId::EnumVariantId(_) => DiagnosticCode::RustcHardError("E0559"), + _ => DiagnosticCode::RustcHardError("E0560"), + }, "no such field", node, ) diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs index bb5c2b791392d..cd5e95cc1e377 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs @@ -293,6 +293,7 @@ fn minicore_smoke_test() { // This should be ignored since we conditionally remove code which creates single item use with braces config.disabled.insert("unused_braces".to_owned()); config.disabled.insert("unused_variables".to_owned()); + config.disabled.insert("remove-unnecessary-else".to_owned()); check_diagnostics_with_config(config, &source); } diff --git a/src/tools/rust-analyzer/crates/ide/src/hover.rs b/src/tools/rust-analyzer/crates/ide/src/hover.rs index 95de3c88c8aca..f9210da5fd48f 100644 --- a/src/tools/rust-analyzer/crates/ide/src/hover.rs +++ b/src/tools/rust-analyzer/crates/ide/src/hover.rs @@ -33,7 +33,8 @@ pub struct HoverConfig { pub keywords: bool, pub format: HoverDocFormat, pub max_trait_assoc_items_count: Option, - pub max_struct_field_count: Option, + pub max_fields_count: Option, + pub max_enum_variants_count: Option, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/render.rs b/src/tools/rust-analyzer/crates/ide/src/hover/render.rs index 3f0fc851344ef..ee96767411a09 100644 --- a/src/tools/rust-analyzer/crates/ide/src/hover/render.rs +++ b/src/tools/rust-analyzer/crates/ide/src/hover/render.rs @@ -411,8 +411,21 @@ pub(super) fn definition( Definition::Trait(trait_) => { trait_.display_limited(db, config.max_trait_assoc_items_count).to_string() } - Definition::Adt(Adt::Struct(struct_)) => { - struct_.display_limited(db, config.max_struct_field_count).to_string() + Definition::Adt(adt @ (Adt::Struct(_) | Adt::Union(_))) => { + adt.display_limited(db, config.max_fields_count).to_string() + } + Definition::Variant(variant) => { + variant.display_limited(db, config.max_fields_count).to_string() + } + Definition::Adt(adt @ Adt::Enum(_)) => { + adt.display_limited(db, config.max_enum_variants_count).to_string() + } + Definition::SelfType(impl_def) => { + let self_ty = &impl_def.self_ty(db); + match self_ty.as_adt() { + Some(adt) => adt.display_limited(db, config.max_fields_count).to_string(), + None => self_ty.display(db).to_string(), + } } Definition::Macro(it) => { let mut label = it.display(db).to_string(); diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs index 6bbc8b380d6c7..f7af7950041d7 100644 --- a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs +++ b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs @@ -18,7 +18,8 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig { format: HoverDocFormat::Markdown, keywords: true, max_trait_assoc_items_count: None, - max_struct_field_count: None, + max_fields_count: Some(5), + max_enum_variants_count: Some(5), }; fn check_hover_no_result(ra_fixture: &str) { @@ -51,13 +52,43 @@ fn check(ra_fixture: &str, expect: Expect) { } #[track_caller] -fn check_hover_struct_limit(count: usize, ra_fixture: &str, expect: Expect) { +fn check_hover_fields_limit( + fields_count: impl Into>, + ra_fixture: &str, + expect: Expect, +) { let (analysis, position) = fixture::position(ra_fixture); let hover = analysis .hover( &HoverConfig { links_in_hover: true, - max_struct_field_count: Some(count), + max_fields_count: fields_count.into(), + ..HOVER_BASE_CONFIG + }, + FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, + ) + .unwrap() + .unwrap(); + + let content = analysis.db.file_text(position.file_id); + let hovered_element = &content[hover.range]; + + let actual = format!("*{hovered_element}*\n{}\n", hover.info.markup); + expect.assert_eq(&actual) +} + +#[track_caller] +fn check_hover_enum_variants_limit( + variants_count: impl Into>, + ra_fixture: &str, + expect: Expect, +) { + let (analysis, position) = fixture::position(ra_fixture); + let hover = analysis + .hover( + &HoverConfig { + links_in_hover: true, + max_enum_variants_count: variants_count.into(), ..HOVER_BASE_CONFIG }, FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, @@ -876,7 +907,9 @@ struct Foo$0 { field: u32 } ```rust // size = 4, align = 4 - struct Foo + struct Foo { + field: u32, + } ``` "#]], ); @@ -896,6 +929,9 @@ struct Foo$0 where u32: Copy { field: u32 } struct Foo where u32: Copy, + { + field: u32, + } ``` "#]], ); @@ -903,7 +939,7 @@ struct Foo$0 where u32: Copy { field: u32 } #[test] fn hover_record_struct_limit() { - check_hover_struct_limit( + check_hover_fields_limit( 3, r#" struct Foo$0 { a: u32, b: i32, c: i32 } @@ -917,7 +953,7 @@ fn hover_record_struct_limit() { ```rust // size = 12 (0xC), align = 4 - struct Foo { + struct Foo { a: u32, b: i32, c: i32, @@ -925,7 +961,7 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_struct_limit( + check_hover_fields_limit( 3, r#" struct Foo$0 { a: u32 } @@ -939,13 +975,13 @@ fn hover_record_struct_limit() { ```rust // size = 4, align = 4 - struct Foo { + struct Foo { a: u32, } ``` "#]], ); - check_hover_struct_limit( + check_hover_fields_limit( 3, r#" struct Foo$0 { a: u32, b: i32, c: i32, d: u32 } @@ -959,7 +995,7 @@ fn hover_record_struct_limit() { ```rust // size = 16 (0x10), align = 4 - struct Foo { + struct Foo { a: u32, b: i32, c: i32, @@ -968,6 +1004,338 @@ fn hover_record_struct_limit() { ``` "#]], ); + check_hover_fields_limit( + None, + r#" + struct Foo$0 { a: u32, b: i32, c: i32 } + "#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 12 (0xC), align = 4 + struct Foo + ``` + "#]], + ); + check_hover_fields_limit( + 0, + r#" + struct Foo$0 { a: u32, b: i32, c: i32 } + "#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 12 (0xC), align = 4 + struct Foo { /* … */ } + ``` + "#]], + ); + + // No extra spaces within `{}` when there are no fields + check_hover_fields_limit( + 5, + r#" + struct Foo$0 {} + "#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 0, align = 1 + struct Foo {} + ``` + "#]], + ); +} + +#[test] +fn hover_record_variant_limit() { + check_hover_fields_limit( + 3, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 12 (0xC), align = 4 + A { a: u32, b: i32, c: i32, } + ``` + "#]], + ); + check_hover_fields_limit( + 3, + r#" + enum Foo { A$0 { a: u32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 4, align = 4 + A { a: u32, } + ``` + "#]], + ); + check_hover_fields_limit( + 3, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32, d: u32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 16 (0x10), align = 4 + A { a: u32, b: i32, c: i32, /* … */ } + ``` + "#]], + ); + check_hover_fields_limit( + None, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 12 (0xC), align = 4 + A + ``` + "#]], + ); + check_hover_fields_limit( + 0, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 12 (0xC), align = 4 + A { /* … */ } + ``` + "#]], + ); +} + +#[test] +fn hover_enum_limit() { + check_hover_enum_variants_limit( + 5, + r#"enum Foo$0 { A, B }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 1, align = 1, niches = 254 + enum Foo { + A, + B, + } + ``` + "#]], + ); + check_hover_enum_variants_limit( + 1, + r#"enum Foo$0 { A, B }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 1, align = 1, niches = 254 + enum Foo { + A, + /* … */ + } + ``` + "#]], + ); + check_hover_enum_variants_limit( + 0, + r#"enum Foo$0 { A, B }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 1, align = 1, niches = 254 + enum Foo { /* … */ } + ``` + "#]], + ); + check_hover_enum_variants_limit( + None, + r#"enum Foo$0 { A, B }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 1, align = 1, niches = 254 + enum Foo + ``` + "#]], + ); + check_hover_enum_variants_limit( + 7, + r#"enum Enum$0 { + Variant {}, + Variant2 { field: i32 }, + Variant3 { field: i32, field2: i32 }, + Variant4(), + Variant5(i32), + Variant6(i32, i32), + Variant7, + Variant8, + }"#, + expect![[r#" + *Enum* + + ```rust + test + ``` + + ```rust + // size = 12 (0xC), align = 4, niches = 4294967288 + enum Enum { + Variant {}, + Variant2 { /* … */ }, + Variant3 { /* … */ }, + Variant4(), + Variant5( /* … */ ), + Variant6( /* … */ ), + Variant7, + /* … */ + } + ``` + "#]], + ); +} + +#[test] +fn hover_union_limit() { + check_hover_fields_limit( + 5, + r#"union Foo$0 { a: u32, b: i32 }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 4, align = 4 + union Foo { + a: u32, + b: i32, + } + ``` + "#]], + ); + check_hover_fields_limit( + 1, + r#"union Foo$0 { a: u32, b: i32 }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 4, align = 4 + union Foo { + a: u32, + /* … */ + } + ``` + "#]], + ); + check_hover_fields_limit( + 0, + r#"union Foo$0 { a: u32, b: i32 }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 4, align = 4 + union Foo { /* … */ } + ``` + "#]], + ); + check_hover_fields_limit( + None, + r#"union Foo$0 { a: u32, b: i32 }"#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 4, align = 4 + union Foo + ``` + "#]], + ); } #[test] @@ -1431,11 +1799,14 @@ impl Thing { ``` ```rust - struct Thing + struct Thing { + x: u32, + } ``` "#]], ); - check( + check_hover_fields_limit( + None, r#" struct Thing { x: u32 } impl Thing { @@ -1456,9 +1827,9 @@ impl Thing { ); check( r#" -enum Thing { A } +struct Thing { x: u32 } impl Thing { - pub fn new() -> Self$0 { Thing::A } + fn new() -> Self$0 { Self { x: 0 } } } "#, expect![[r#" @@ -1469,8 +1840,8 @@ impl Thing { ``` ```rust - enum Thing { - A, + struct Thing { + x: u32, } ``` "#]], @@ -1478,23 +1849,44 @@ impl Thing { check( r#" enum Thing { A } +impl Thing { + pub fn new() -> Self$0 { Thing::A } +} +"#, + expect![[r#" + *Self* + + ```rust + test + ``` + + ```rust + enum Thing { + A, + } + ``` + "#]], + ); + check( + r#" +enum Thing { A } impl Thing { pub fn thing(a: Self$0) {} } "#, expect![[r#" - *Self* + *Self* - ```rust - test - ``` + ```rust + test + ``` - ```rust - enum Thing { - A, - } - ``` - "#]], + ```rust + enum Thing { + A, + } + ``` + "#]], ); check( r#" @@ -2382,8 +2774,8 @@ fn test_hover_layout_of_enum() { ```rust // size = 16 (0x10), align = 8, niches = 254 enum Foo { - Variant1(u8, u16), - Variant2(i32, u8, i64), + Variant1( /* … */ ), + Variant2( /* … */ ), } ``` "#]], @@ -6554,7 +6946,7 @@ enum Enum { ```rust // size = 4, align = 4 - RecordV { field: u32 } + RecordV { field: u32, } ``` "#]], ); @@ -7864,8 +8256,8 @@ impl Iterator for S { file_id: FileId( 1, ), - full_range: 6290..6498, - focus_range: 6355..6361, + full_range: 7791..7999, + focus_range: 7856..7862, name: "Future", kind: Trait, container_name: "future", @@ -7878,8 +8270,8 @@ impl Iterator for S { file_id: FileId( 1, ), - full_range: 7128..7594, - focus_range: 7172..7180, + full_range: 8629..9095, + focus_range: 8673..8681, name: "Iterator", kind: Trait, container_name: "iterator", @@ -7936,7 +8328,9 @@ struct Pedro$0<'a> { ```rust // size = 16 (0x10), align = 8, niches = 1 - struct Pedro<'a> + struct Pedro<'a> { + hola: &str, + } ``` "#]], ) diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs index 07b9f9cc1fff6..0cb8c485b2f27 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs @@ -331,6 +331,25 @@ fn main(a: SliceIter<'_, Container>) { ); } + #[test] + fn lt_hints() { + check_types( + r#" +struct S<'lt>; + +fn f<'a>() { + let x = S::<'static>; + //^ S<'static> + let y = S::<'_>; + //^ S + let z = S::<'a>; + //^ S<'a> + +} +"#, + ); + } + #[test] fn fn_hints() { check_types( @@ -341,7 +360,7 @@ fn foo1() -> impl Fn(f64) { loop {} } fn foo2() -> impl Fn(f64, f64) { loop {} } fn foo3() -> impl Fn(f64, f64) -> u32 { loop {} } fn foo4() -> &'static dyn Fn(f64, f64) -> u32 { loop {} } -fn foo5() -> &'static dyn Fn(&'static dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} } +fn foo5() -> &'static for<'a> dyn Fn(&'a dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} } fn foo6() -> impl Fn(f64, f64) -> u32 + Sized { loop {} } fn foo7() -> *const (impl Fn(f64, f64) -> u32 + Sized) { loop {} } diff --git a/src/tools/rust-analyzer/crates/ide/src/static_index.rs b/src/tools/rust-analyzer/crates/ide/src/static_index.rs index ca013da70990f..0d2311b6e9844 100644 --- a/src/tools/rust-analyzer/crates/ide/src/static_index.rs +++ b/src/tools/rust-analyzer/crates/ide/src/static_index.rs @@ -167,7 +167,8 @@ impl StaticIndex<'_> { keywords: true, format: crate::HoverDocFormat::Markdown, max_trait_assoc_items_count: None, - max_struct_field_count: None, + max_fields_count: Some(5), + max_enum_variants_count: Some(5), }; let tokens = tokens.filter(|token| { matches!( diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html index 366895ce7ec70..893e3c0675181 100644 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html +++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html @@ -49,5 +49,5 @@ fn main() { let foo = Some(92); - let nums = iter::repeat(foo.unwrap()); + let nums = iter::repeat(foo.unwrap()); } \ No newline at end of file diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index 7a07d17b2718d..22706dea1fa6c 100644 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -158,9 +158,9 @@ println!("{ничоси}", ничоси = 92); println!("{:x?} {} ", thingy, n2); - panic!("{}", 0); + panic!("{}", 0); panic!("more {}", 1); - assert!(true, "{}", 1); + assert!(true, "{}", 1); assert!(true, "{} asdasd", 1); toho!("{}fmt", 0); let i: u64 = 3; diff --git a/src/tools/rust-analyzer/crates/load-cargo/src/lib.rs b/src/tools/rust-analyzer/crates/load-cargo/src/lib.rs index 31b0c8cdec50e..c859941abd2b9 100644 --- a/src/tools/rust-analyzer/crates/load-cargo/src/lib.rs +++ b/src/tools/rust-analyzer/crates/load-cargo/src/lib.rs @@ -17,6 +17,7 @@ use itertools::Itertools; use proc_macro_api::{MacroDylib, ProcMacroServer}; use project_model::{CargoConfig, PackageRoot, ProjectManifest, ProjectWorkspace}; use span::Span; +use tracing::{instrument, Level}; use vfs::{file_set::FileSetConfig, loader::Handle, AbsPath, AbsPathBuf, VfsPath}; pub struct LoadCargoConfig { @@ -50,6 +51,7 @@ pub fn load_workspace_at( load_workspace(workspace, &cargo_config.extra_env, load_config) } +#[instrument(skip_all)] pub fn load_workspace( ws: ProjectWorkspace, extra_env: &FxHashMap, @@ -66,9 +68,9 @@ pub fn load_workspace( let proc_macro_server = match &load_config.with_proc_macro_server { ProcMacroServerChoice::Sysroot => ws .find_sysroot_proc_macro_srv() - .and_then(|it| ProcMacroServer::spawn(it, extra_env).map_err(Into::into)), + .and_then(|it| ProcMacroServer::spawn(&it, extra_env).map_err(Into::into)), ProcMacroServerChoice::Explicit(path) => { - ProcMacroServer::spawn(path.clone(), extra_env).map_err(Into::into) + ProcMacroServer::spawn(path, extra_env).map_err(Into::into) } ProcMacroServerChoice::None => Err(anyhow::format_err!("proc macro server disabled")), }; @@ -333,9 +335,7 @@ fn load_crate_graph( vfs: &mut vfs::Vfs, receiver: &Receiver, ) -> RootDatabase { - let (ProjectWorkspace::Cargo { toolchain, target_layout, .. } - | ProjectWorkspace::Json { toolchain, target_layout, .. } - | ProjectWorkspace::DetachedFile { toolchain, target_layout, .. }) = ws; + let ProjectWorkspace { toolchain, target_layout, .. } = ws; let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::().ok()); let mut db = RootDatabase::new(lru_cap); @@ -352,6 +352,7 @@ fn load_crate_graph( } } vfs::loader::Message::Loaded { files } | vfs::loader::Message::Changed { files } => { + let _p = tracing::span!(Level::INFO, "LoadCargo::load_file_contents").entered(); for (path, contents) in files { vfs.set_file_contents(path.into(), contents); } diff --git a/src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs b/src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs index 0ab16c38c87ba..c0b27397cbfa0 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs @@ -13,7 +13,7 @@ mod version; use base_db::Env; use indexmap::IndexSet; -use paths::AbsPathBuf; +use paths::{AbsPath, AbsPathBuf}; use rustc_hash::FxHashMap; use span::Span; use std::{ @@ -54,6 +54,7 @@ pub struct ProcMacroServer { /// /// Therefore, we just wrap the `ProcMacroProcessSrv` in a mutex here. process: Arc>, + path: AbsPathBuf, } pub struct MacroDylib { @@ -113,11 +114,18 @@ pub struct MacroPanic { impl ProcMacroServer { /// Spawns an external process as the proc macro server and returns a client connected to it. pub fn spawn( - process_path: AbsPathBuf, + process_path: &AbsPath, env: &FxHashMap, ) -> io::Result { let process = ProcMacroProcessSrv::run(process_path, env)?; - Ok(ProcMacroServer { process: Arc::new(Mutex::new(process)) }) + Ok(ProcMacroServer { + process: Arc::new(Mutex::new(process)), + path: process_path.to_owned(), + }) + } + + pub fn path(&self) -> &AbsPath { + &self.path } pub fn load_dylib(&self, dylib: MacroDylib) -> Result, ServerError> { diff --git a/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs b/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs index 35d48a155433f..dce086d4299be 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs @@ -6,7 +6,7 @@ use std::{ sync::Arc, }; -use paths::{AbsPath, AbsPathBuf}; +use paths::AbsPath; use rustc_hash::FxHashMap; use stdx::JodChild; @@ -28,11 +28,11 @@ pub(crate) struct ProcMacroProcessSrv { impl ProcMacroProcessSrv { pub(crate) fn run( - process_path: AbsPathBuf, + process_path: &AbsPath, env: &FxHashMap, ) -> io::Result { let create_srv = |null_stderr| { - let mut process = Process::run(process_path.clone(), env, null_stderr)?; + let mut process = Process::run(process_path, env, null_stderr)?; let (stdin, stdout) = process.stdio().expect("couldn't access child stdio"); io::Result::Ok(ProcMacroProcessSrv { @@ -153,11 +153,11 @@ struct Process { impl Process { fn run( - path: AbsPathBuf, + path: &AbsPath, env: &FxHashMap, null_stderr: bool, ) -> io::Result { - let child = JodChild(mk_child(&path, env, null_stderr)?); + let child = JodChild(mk_child(path, env, null_stderr)?); Ok(Process { child }) } diff --git a/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs b/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs index fbd423c9eac8a..4dc3b3f349895 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs @@ -24,7 +24,7 @@ use toolchain::Tool; use crate::{ cfg::CfgFlag, utf8_stdout, CargoConfig, CargoFeatures, CargoWorkspace, InvocationLocation, - InvocationStrategy, Package, Sysroot, TargetKind, + InvocationStrategy, ManifestPath, Package, Sysroot, TargetKind, }; /// Output of the build script and proc-macro building steps for a workspace. @@ -63,7 +63,7 @@ impl WorkspaceBuildScripts { fn build_command( config: &CargoConfig, allowed_features: &FxHashSet, - workspace_root: &AbsPathBuf, + manifest_path: &ManifestPath, sysroot: Option<&Sysroot>, ) -> io::Result { let mut cmd = match config.run_build_script_command.as_deref() { @@ -79,7 +79,7 @@ impl WorkspaceBuildScripts { cmd.args(&config.extra_args); cmd.arg("--manifest-path"); - cmd.arg(workspace_root.join("Cargo.toml")); + cmd.arg(manifest_path.as_ref()); if let Some(target_dir) = &config.target_dir { cmd.arg("--target-dir").arg(target_dir); @@ -116,6 +116,10 @@ impl WorkspaceBuildScripts { } } + if manifest_path.is_rust_manifest() { + cmd.arg("-Zscript"); + } + cmd } }; @@ -152,37 +156,12 @@ impl WorkspaceBuildScripts { .as_ref(); let allowed_features = workspace.workspace_features(); - - match Self::run_per_ws( - Self::build_command( - config, - &allowed_features, - &workspace.workspace_root().to_path_buf(), - sysroot, - )?, - workspace, - current_dir, - progress, - ) { - Ok(WorkspaceBuildScripts { error: Some(error), .. }) - if toolchain.as_ref().map_or(false, |it| *it >= RUST_1_75) => - { - // building build scripts failed, attempt to build with --keep-going so - // that we potentially get more build data - let mut cmd = Self::build_command( - config, - &allowed_features, - &workspace.workspace_root().to_path_buf(), - sysroot, - )?; - - cmd.args(["--keep-going"]); - let mut res = Self::run_per_ws(cmd, workspace, current_dir, progress)?; - res.error = Some(error); - Ok(res) - } - res => res, + let mut cmd = + Self::build_command(config, &allowed_features, workspace.manifest_path(), sysroot)?; + if toolchain.as_ref().map_or(false, |it| *it >= RUST_1_75) { + cmd.args(["--keep-going"]); } + Self::run_per_ws(cmd, workspace, current_dir, progress) } /// Runs the build scripts by invoking the configured command *once*. @@ -204,7 +183,13 @@ impl WorkspaceBuildScripts { )) } }; - let cmd = Self::build_command(config, &Default::default(), workspace_root, None)?; + let cmd = Self::build_command( + config, + &Default::default(), + // This is not gonna be used anyways, so just construct a dummy here + &ManifestPath::try_from(workspace_root.clone()).unwrap(), + None, + )?; // NB: Cargo.toml could have been modified between `cargo metadata` and // `cargo check`. We shouldn't assume that package ids we see here are // exactly those from `config`. diff --git a/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs b/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs index ff7cf144aa82d..9955f2687c917 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs @@ -32,6 +32,7 @@ pub struct CargoWorkspace { targets: Arena, workspace_root: AbsPathBuf, target_directory: AbsPathBuf, + manifest_path: ManifestPath, } impl ops::Index for CargoWorkspace { @@ -306,7 +307,7 @@ impl CargoWorkspace { ); } // The manifest is a rust file, so this means its a script manifest - if cargo_toml.extension().is_some_and(|ext| ext == "rs") { + if cargo_toml.is_rust_manifest() { // Deliberately don't set up RUSTC_BOOTSTRAP or a nightly override here, the user should // opt into it themselves. other_options.push("-Zscript".to_owned()); @@ -334,7 +335,7 @@ impl CargoWorkspace { .with_context(|| format!("Failed to run `{:?}`", meta.cargo_command())) } - pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace { + pub fn new(mut meta: cargo_metadata::Metadata, manifest_path: ManifestPath) -> CargoWorkspace { let mut pkg_by_id = FxHashMap::default(); let mut packages = Arena::default(); let mut targets = Arena::default(); @@ -448,7 +449,7 @@ impl CargoWorkspace { let target_directory = AbsPathBuf::assert(meta.target_directory); - CargoWorkspace { packages, targets, workspace_root, target_directory } + CargoWorkspace { packages, targets, workspace_root, target_directory, manifest_path } } pub fn packages(&self) -> impl ExactSizeIterator + '_ { @@ -466,6 +467,10 @@ impl CargoWorkspace { &self.workspace_root } + pub fn manifest_path(&self) -> &ManifestPath { + &self.manifest_path + } + pub fn target_directory(&self) -> &AbsPath { &self.target_directory } diff --git a/src/tools/rust-analyzer/crates/project-model/src/env.rs b/src/tools/rust-analyzer/crates/project-model/src/env.rs index 762e01c917724..5520cdaff6b00 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/env.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/env.rs @@ -60,16 +60,19 @@ pub(crate) fn inject_rustc_tool_env(env: &mut Env, cargo_name: &str, kind: Targe } pub(crate) fn cargo_config_env( - cargo_toml: &ManifestPath, + manifest: &ManifestPath, extra_env: &FxHashMap, sysroot: Option<&Sysroot>, ) -> FxHashMap { let mut cargo_config = Sysroot::tool(sysroot, Tool::Cargo); cargo_config.envs(extra_env); cargo_config - .current_dir(cargo_toml.parent()) + .current_dir(manifest.parent()) .args(["-Z", "unstable-options", "config", "get", "env"]) .env("RUSTC_BOOTSTRAP", "1"); + if manifest.is_rust_manifest() { + cargo_config.arg("-Zscript"); + } // if successful we receive `env.key.value = "value" per entry tracing::debug!("Discovering cargo config env by {:?}", cargo_config); utf8_stdout(cargo_config).map(parse_output_cargo_config_env).unwrap_or_default() diff --git a/src/tools/rust-analyzer/crates/project-model/src/lib.rs b/src/tools/rust-analyzer/crates/project-model/src/lib.rs index 7f3e35ca5dbca..181c07f46b27d 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/lib.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/lib.rs @@ -52,13 +52,15 @@ pub use crate::{ manifest_path::ManifestPath, project_json::{ProjectJson, ProjectJsonData}, sysroot::Sysroot, - workspace::{FileLoader, PackageRoot, ProjectWorkspace}, + workspace::{FileLoader, PackageRoot, ProjectWorkspace, ProjectWorkspaceKind}, }; +pub use cargo_metadata::Metadata; #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub enum ProjectManifest { ProjectJson(ManifestPath), CargoToml(ManifestPath), + CargoScript(ManifestPath), } impl ProjectManifest { @@ -71,7 +73,10 @@ impl ProjectManifest { if path.file_name().unwrap_or_default() == "Cargo.toml" { return Ok(ProjectManifest::CargoToml(path)); } - bail!("project root must point to Cargo.toml or rust-project.json: {path}"); + if path.extension().unwrap_or_default() == "rs" { + return Ok(ProjectManifest::CargoScript(path)); + } + bail!("project root must point to a Cargo.toml, rust-project.json or