From f521e4185323699cd5d063b2704367a319583982 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:02:40 +0300 Subject: internal: introduce minicore -- a subset of libcore for testing --- crates/base_db/src/fixture.rs | 31 ++++- crates/hir_ty/src/tests/coercion.rs | 47 +++---- crates/rust-analyzer/tests/slow-tests/support.rs | 4 +- crates/test_utils/src/fixture.rs | 157 +++++++++++++++++++++-- crates/test_utils/src/lib.rs | 5 +- crates/test_utils/src/minicore.rs | 69 ++++++++++ 6 files changed, 271 insertions(+), 42 deletions(-) create mode 100644 crates/test_utils/src/minicore.rs (limited to 'crates') diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 1b17db102..d56b20b83 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs @@ -9,8 +9,8 @@ use test_utils::{ use vfs::{file_set::FileSet, VfsPath}; use crate::{ - input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, FileRange, - SourceDatabaseExt, SourceRoot, SourceRootId, + input::CrateName, Change, CrateDisplayName, CrateGraph, CrateId, Edition, Env, FileId, + FilePosition, FileRange, SourceDatabaseExt, SourceRoot, SourceRootId, }; pub const WORKSPACE: SourceRootId = SourceRootId(0); @@ -81,7 +81,7 @@ pub struct ChangeFixture { impl ChangeFixture { pub fn parse(ra_fixture: &str) -> ChangeFixture { - let fixture = Fixture::parse(ra_fixture); + let (mini_core, fixture) = Fixture::parse(ra_fixture); let mut change = Change::new(); let mut files = Vec::new(); @@ -166,6 +166,31 @@ impl ChangeFixture { } } + if let Some(mini_core) = mini_core { + let core_file = file_id; + file_id.0 += 1; + + let mut fs = FileSet::default(); + fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string())); + roots.push(SourceRoot::new_library(fs)); + + change.change_file(core_file, Some(Arc::new(mini_core.source_code()))); + + let all_crates = crate_graph.crates_in_topological_order(); + + let core_crate = crate_graph.add_crate_root( + core_file, + Edition::Edition2021, + Some(CrateDisplayName::from_canonical_name("core".to_string())), + CfgOptions::default(), + Env::default(), + Vec::new(), + ); + + for krate in all_crates { + crate_graph.add_dep(krate, CrateName::new("core").unwrap(), core_crate).unwrap(); + } + } roots.push(SourceRoot::new_local(mem::take(&mut file_set))); change.set_roots(roots); change.set_crate_graph(crate_graph); diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index 4f859fc85..91236e974 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -23,38 +23,29 @@ fn infer_block_expr_type_mismatch() { fn coerce_places() { check_infer( r#" - struct S { a: T } +//- minicore: coerce_unsized +struct S { a: T } - fn f(_: &[T]) -> T { loop {} } - fn g(_: S<&[T]>) -> T { loop {} } +fn f(_: &[T]) -> T { loop {} } +fn g(_: S<&[T]>) -> T { loop {} } - fn gen() -> *mut [T; 2] { loop {} } - fn test1() -> *mut [U] { - gen() - } - - fn test2() { - let arr: &[u8; 1] = &[1]; +fn gen() -> *mut [T; 2] { loop {} } +fn test1() -> *mut [U] { + gen() +} - let a: &[_] = arr; - let b = f(arr); - let c: &[_] = { arr }; - let d = g(S { a: arr }); - let e: [&[_]; 1] = [arr]; - let f: [&[_]; 2] = [arr; 2]; - let g: (&[_], &[_]) = (arr, arr); - } +fn test2() { + let arr: &[u8; 1] = &[1]; - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} - impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} - "#, + let a: &[_] = arr; + let b = f(arr); + let c: &[_] = { arr }; + let d = g(S { a: arr }); + let e: [&[_]; 1] = [arr]; + let f: [&[_]; 2] = [arr; 2]; + let g: (&[_], &[_]) = (arr, arr); +} +"#, expect![[r#" 30..31 '_': &[T] 44..55 '{ loop {} }': T diff --git a/crates/rust-analyzer/tests/slow-tests/support.rs b/crates/rust-analyzer/tests/slow-tests/support.rs index e22c295f9..260a504e7 100644 --- a/crates/rust-analyzer/tests/slow-tests/support.rs +++ b/crates/rust-analyzer/tests/slow-tests/support.rs @@ -75,7 +75,9 @@ impl<'a> Project<'a> { profile::init_from(crate::PROFILE); }); - for entry in Fixture::parse(self.fixture) { + let (mini_core, fixtures) = Fixture::parse(self.fixture); + assert!(mini_core.is_none()); + for entry in fixtures { let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path.as_path(), entry.text.as_bytes()).unwrap(); diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index d0bddf7d8..535892f3f 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -77,6 +77,11 @@ pub struct Fixture { pub introduce_new_source_root: bool, } +pub struct MiniCore { + activated_flags: Vec, + valid_flags: Vec, +} + impl Fixture { /// Parses text which looks like this: /// @@ -86,12 +91,28 @@ impl Fixture { /// line 2 /// //- other meta /// ``` - pub fn parse(ra_fixture: &str) -> Vec { + /// + /// Fixture can also start with a minicore declaration: + /// + /// ``` + /// //- minicore: sized + /// ``` + /// + /// That will include a subset of `libcore` into the fixture, see + /// `minicore.rs` for what's available. + pub fn parse(ra_fixture: &str) -> (Option, Vec) { let fixture = trim_indent(ra_fixture); - + let mut fixture = fixture.as_str(); + let mut mini_core = None; let mut res: Vec = Vec::new(); - let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") }; + if fixture.starts_with("//- minicore:") { + let first_line = fixture.split_inclusive('\n').next().unwrap(); + mini_core = Some(MiniCore::parse(first_line)); + fixture = &fixture[first_line.len()..]; + } + + let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") }; for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() { if line.contains("//-") { @@ -113,7 +134,7 @@ impl Fixture { } } - res + (mini_core, res) } //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo @@ -172,6 +193,122 @@ impl Fixture { } } +impl MiniCore { + fn has_flag(&self, flag: &str) -> bool { + self.activated_flags.iter().any(|it| it == flag) + } + + fn assert_valid_flag(&self, flag: &str) { + if !self.valid_flags.iter().any(|it| it == flag) { + panic!("invalid flag: {:?}, valid flags: {:?}", flag, self.valid_flags); + } + } + + fn parse(line: &str) -> MiniCore { + let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() }; + + let line = line.strip_prefix("//- minicore:").unwrap().trim(); + for entry in line.split(", ") { + if res.has_flag(entry) { + panic!("duplicate minicore flag: {:?}", entry) + } + res.activated_flags.push(entry.to_string()) + } + + res + } + + /// Strips parts of minicore.rs which are flagged by inactive flags. + /// + /// This is probably over-engineered to support flags dependencies. + pub fn source_code(mut self) -> String { + let mut buf = String::new(); + let raw_mini_core = include_str!("./minicore.rs"); + let mut lines = raw_mini_core.split_inclusive('\n'); + + let mut parsing_flags = false; + let mut implications = Vec::new(); + + // Parse `//!` preamble and extract flags and dependencies. + for line in lines.by_ref() { + let line = match line.strip_prefix("//!") { + Some(it) => it, + None => { + assert!(line.trim().is_empty()); + break; + } + }; + + if parsing_flags { + let (flag, deps) = line.split_once(':').unwrap(); + let flag = flag.trim(); + self.valid_flags.push(flag.to_string()); + for dep in deps.split(", ") { + let dep = dep.trim(); + if !dep.is_empty() { + self.assert_valid_flag(dep); + implications.push((flag, dep)); + } + } + } + + if line.contains("Available flags:") { + parsing_flags = true; + } + } + + for flag in &self.activated_flags { + self.assert_valid_flag(flag); + } + + // Fixed point loop to compute transitive closure of flags. + loop { + let mut changed = false; + for &(u, v) in implications.iter() { + if self.has_flag(u) && !self.has_flag(v) { + self.activated_flags.push(v.to_string()); + changed = true; + } + } + if !changed { + break; + } + } + + let mut curr_region = ""; + for line in lines { + let trimmed = line.trim(); + if let Some(region) = trimmed.strip_prefix("// region:") { + assert_eq!(curr_region, ""); + curr_region = region; + continue; + } + if let Some(region) = trimmed.strip_prefix("// endregion:") { + assert_eq!(curr_region, region); + curr_region = ""; + continue; + } + + let mut flag = curr_region; + if let Some(idx) = trimmed.find("// :") { + flag = &trimmed[idx + "// :".len()..]; + } + + let skip = if flag == "" { + false + } else { + self.assert_valid_flag(flag); + !self.has_flag(flag) + }; + + if !skip { + buf.push_str(line) + } + } + buf + } +} + #[test] #[should_panic] fn parse_fixture_checks_further_indented_metadata() { @@ -189,12 +326,14 @@ fn parse_fixture_checks_further_indented_metadata() { #[test] fn parse_fixture_gets_full_meta() { - let parsed = Fixture::parse( - r" - //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo - mod m; - ", + let (mini_core, parsed) = Fixture::parse( + r#" +//- minicore: coerce_unsized +//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo +mod m; +"#, ); + assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_string()]); assert_eq!(1, parsed.len()); let meta = &parsed[0]; diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index b2fe25f82..d55bae62a 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -23,7 +23,10 @@ use text_size::{TextRange, TextSize}; pub use dissimilar::diff as __diff; pub use rustc_hash::FxHashMap; -pub use crate::{assert_linear::AssertLinear, fixture::Fixture}; +pub use crate::{ + assert_linear::AssertLinear, + fixture::{Fixture, MiniCore}, +}; pub const CURSOR_MARKER: &str = "$0"; pub const ESCAPED_CURSOR_MARKER: &str = "\\$0"; diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs new file mode 100644 index 000000000..629c06ed0 --- /dev/null +++ b/crates/test_utils/src/minicore.rs @@ -0,0 +1,69 @@ +//! This is a fixture we use for tests that need lang items. +//! +//! We want to include the minimal subset of core for each test, so this file +//! supports "conditional compilation". Tests use the following syntax to include minicore: +//! +//! //- minicore: flag1, flag2 +//! +//! We then strip all the code marked with other flags. +//! +//! Available flags: +//! sized: +//! coerce_unsized: sized + +pub mod marker { + // region:sized + #[lang = "sized"] + #[fundamental] + #[rustc_specialization_trait] + pub trait Sized {} + + #[lang = "unsize"] + pub trait Unsize {} + // endregion:sized +} + +pub mod ops { + mod unsize { + // region:coerce_unsized + use crate::marker::Unsize; + + #[lang = "coerce_unsized"] + pub trait CoerceUnsized {} + + impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {} + impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {} + impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {} + impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {} + + impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} + impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<*const U> for &'a T {} + + impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} + impl, U: ?Sized> CoerceUnsized<*const U> for *mut T {} + impl, U: ?Sized> CoerceUnsized<*const U> for *const T {} + // endregion:coerce_unsized + } +} + +pub mod prelude { + pub mod v1 { + pub use crate::marker::Sized; // :sized + } + + pub mod rust_2015 { + pub use super::v1::*; + } + + pub mod rust_2018 { + pub use super::v1::*; + } + + pub mod rust_2021 { + pub use super::v1::*; + } +} + +#[prelude_import] +#[allow(unused)] +use prelude::v1::*; -- cgit v1.2.3 From f841369fee0f49125c83046340b228b032ebc702 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:11:53 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/coercion.rs | 197 +++++++++++++++--------------------- crates/test_utils/src/minicore.rs | 9 +- 2 files changed, 88 insertions(+), 118 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index 91236e974..58d454a0f 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -122,60 +122,52 @@ fn infer_let_stmt_coerce() { fn infer_custom_coerce_unsized() { check_infer( r#" - struct A(*const T); - struct B(*const T); - struct C { inner: *const T } - - impl, U: ?Sized> CoerceUnsized> for B {} - impl, U: ?Sized> CoerceUnsized> for C {} +//- minicore: coerce_unsized +use core::{marker::Unsize, ops::CoerceUnsized}; - fn foo1(x: A<[T]>) -> A<[T]> { x } - fn foo2(x: B<[T]>) -> B<[T]> { x } - fn foo3(x: C<[T]>) -> C<[T]> { x } +struct A(*const T); +struct B(*const T); +struct C { inner: *const T } - fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) { - let d = foo1(a); - let e = foo2(b); - let f = foo3(c); - } +impl, U: ?Sized> CoerceUnsized> for B {} +impl, U: ?Sized> CoerceUnsized> for C {} +fn foo1(x: A<[T]>) -> A<[T]> { x } +fn foo2(x: B<[T]>) -> B<[T]> { x } +fn foo3(x: C<[T]>) -> C<[T]> { x } - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} - impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} - "#, +fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) { + let d = foo1(a); + let e = foo2(b); + let f = foo3(c); +} +"#, expect![[r#" - 257..258 'x': A<[T]> - 278..283 '{ x }': A<[T]> - 280..281 'x': A<[T]> - 295..296 'x': B<[T]> - 316..321 '{ x }': B<[T]> - 318..319 'x': B<[T]> - 333..334 'x': C<[T]> - 354..359 '{ x }': C<[T]> - 356..357 'x': C<[T]> - 369..370 'a': A<[u8; 2]> - 384..385 'b': B<[u8; 2]> - 399..400 'c': C<[u8; 2]> - 414..480 '{ ...(c); }': () - 424..425 'd': A<[{unknown}]> - 428..432 'foo1': fn foo1<{unknown}>(A<[{unknown}]>) -> A<[{unknown}]> - 428..435 'foo1(a)': A<[{unknown}]> - 433..434 'a': A<[u8; 2]> - 445..446 'e': B<[u8]> - 449..453 'foo2': fn foo2(B<[u8]>) -> B<[u8]> - 449..456 'foo2(b)': B<[u8]> - 454..455 'b': B<[u8; 2]> - 466..467 'f': C<[u8]> - 470..474 'foo3': fn foo3(C<[u8]>) -> C<[u8]> - 470..477 'foo3(c)': C<[u8]> - 475..476 'c': C<[u8; 2]> + 306..307 'x': A<[T]> + 327..332 '{ x }': A<[T]> + 329..330 'x': A<[T]> + 344..345 'x': B<[T]> + 365..370 '{ x }': B<[T]> + 367..368 'x': B<[T]> + 382..383 'x': C<[T]> + 403..408 '{ x }': C<[T]> + 405..406 'x': C<[T]> + 418..419 'a': A<[u8; 2]> + 433..434 'b': B<[u8; 2]> + 448..449 'c': C<[u8; 2]> + 463..529 '{ ...(c); }': () + 473..474 'd': A<[{unknown}]> + 477..481 'foo1': fn foo1<{unknown}>(A<[{unknown}]>) -> A<[{unknown}]> + 477..484 'foo1(a)': A<[{unknown}]> + 482..483 'a': A<[u8; 2]> + 494..495 'e': B<[u8]> + 498..502 'foo2': fn foo2(B<[u8]>) -> B<[u8]> + 498..505 'foo2(b)': B<[u8]> + 503..504 'b': B<[u8; 2]> + 515..516 'f': C<[u8]> + 519..523 'foo3': fn foo3(C<[u8]>) -> C<[u8]> + 519..526 'foo3(c)': C<[u8]> + 524..525 'c': C<[u8; 2]> "#]], ); } @@ -184,21 +176,16 @@ fn infer_custom_coerce_unsized() { fn infer_if_coerce() { check_infer( r#" - fn foo(x: &[T]) -> &[T] { loop {} } - fn test() { - let x = if true { - foo(&[1]) - } else { - &[1] - }; - } - - - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - "#, +//- minicore: unsize +fn foo(x: &[T]) -> &[T] { loop {} } +fn test() { + let x = if true { + foo(&[1]) + } else { + &[1] + }; +} +"#, expect![[r#" 10..11 'x': &[T] 27..38 '{ loop {} }': &[T] @@ -226,25 +213,16 @@ fn infer_if_coerce() { fn infer_if_else_coerce() { check_infer( r#" - fn foo(x: &[T]) -> &[T] { loop {} } - fn test() { - let x = if true { - &[1] - } else { - foo(&[1]) - }; - } - - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} - impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} - "#, +//- minicore: coerce_unsized +fn foo(x: &[T]) -> &[T] { loop {} } +fn test() { + let x = if true { + &[1] + } else { + foo(&[1]) + }; +} +"#, expect![[r#" 10..11 'x': &[T] 27..38 '{ loop {} }': &[T] @@ -272,20 +250,16 @@ fn infer_if_else_coerce() { fn infer_match_first_coerce() { check_infer( r#" - fn foo(x: &[T]) -> &[T] { loop {} } - fn test(i: i32) { - let x = match i { - 2 => foo(&[2]), - 1 => &[1], - _ => &[3], - }; - } - - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - "#, +//- minicore: unsize +fn foo(x: &[T]) -> &[T] { loop {} } +fn test(i: i32) { + let x = match i { + 2 => foo(&[2]), + 1 => &[1], + _ => &[3], + }; +} +"#, expect![[r#" 10..11 'x': &[T] 27..38 '{ loop {} }': &[T] @@ -320,25 +294,16 @@ fn infer_match_first_coerce() { fn infer_match_second_coerce() { check_infer( r#" - fn foo(x: &[T]) -> &[T] { loop {} } - fn test(i: i32) { - let x = match i { - 1 => &[1], - 2 => foo(&[2]), - _ => &[3], - }; - } - - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} - impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} - "#, +//- minicore: coerce_unsized +fn foo(x: &[T]) -> &[T] { loop {} } +fn test(i: i32) { + let x = match i { + 1 => &[1], + 2 => foo(&[2]), + _ => &[3], + }; +} +"#, expect![[r#" 10..11 'x': &[T] 27..38 '{ loop {} }': &[T] diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 629c06ed0..8f8f1c9f8 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -9,7 +9,8 @@ //! //! Available flags: //! sized: -//! coerce_unsized: sized +//! unsize: sized +//! coerce_unsized: unsize pub mod marker { // region:sized @@ -17,10 +18,12 @@ pub mod marker { #[fundamental] #[rustc_specialization_trait] pub trait Sized {} + // endregion:sized + // region:unsize #[lang = "unsize"] pub trait Unsize {} - // endregion:sized + // endregion:unsize } pub mod ops { @@ -44,6 +47,8 @@ pub mod ops { impl, U: ?Sized> CoerceUnsized<*const U> for *const T {} // endregion:coerce_unsized } + + pub use self::unsize::CoerceUnsized; // :coerce_unsized } pub mod prelude { -- cgit v1.2.3 From 047520153878ade5b0136bc151fc53a43419b1c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:18:26 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/coercion.rs | 310 +++++++++++++++--------------------- 1 file changed, 129 insertions(+), 181 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index 58d454a0f..eca6ae1fe 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -630,25 +630,19 @@ fn coerce_placeholder_ref() { fn coerce_unsize_array() { check_infer_with_mismatches( r#" - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl, U> CoerceUnsized<&U> for &T {} - - fn test() { - let f: &[usize] = &[1, 2, 3]; - } +//- minicore: coerce_unsized +fn test() { + let f: &[usize] = &[1, 2, 3]; +} "#, expect![[r#" - 161..198 '{ ... 3]; }': () - 171..172 'f': &[usize] - 185..195 '&[1, 2, 3]': &[usize; 3] - 186..195 '[1, 2, 3]': [usize; 3] - 187..188 '1': usize - 190..191 '2': usize - 193..194 '3': usize + 10..47 '{ ... 3]; }': () + 20..21 'f': &[usize] + 34..44 '&[1, 2, 3]': &[usize; 3] + 35..44 '[1, 2, 3]': [usize; 3] + 36..37 '1': usize + 39..40 '2': usize + 42..43 '3': usize "#]], ); } @@ -657,42 +651,34 @@ fn coerce_unsize_array() { fn coerce_unsize_trait_object_simple() { check_infer_with_mismatches( r#" - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl, U> CoerceUnsized<&U> for &T {} - - trait Foo {} - trait Bar: Foo {} - trait Baz: Bar {} +//- minicore: coerce_unsized +trait Foo {} +trait Bar: Foo {} +trait Baz: Bar {} - struct S; - impl Foo for S {} - impl Bar for S {} - impl Baz for S {} +struct S; +impl Foo for S {} +impl Bar for S {} +impl Baz for S {} - fn test() { - let obj: &dyn Baz = &S; - let obj: &dyn Bar<_, i8, i16> = &S; - let obj: &dyn Foo = &S; - } - "#, - expect![[r" - 424..539 '{ ... &S; }': () - 434..437 'obj': &dyn Baz - 459..461 '&S': &S - 460..461 'S': S - 471..474 'obj': &dyn Bar - 499..501 '&S': &S - 500..501 'S': S - 511..514 'obj': &dyn Foo - 534..536 '&S': &S - 535..536 'S': S - "]], +fn test() { + let obj: &dyn Baz = &S; + let obj: &dyn Bar<_, i8, i16> = &S; + let obj: &dyn Foo = &S; +} +"#, + expect![[r#" + 236..351 '{ ... &S; }': () + 246..249 'obj': &dyn Baz + 271..273 '&S': &S + 272..273 'S': S + 283..286 'obj': &dyn Bar + 311..313 '&S': &S + 312..313 'S': S + 323..326 'obj': &dyn Foo + 346..348 '&S': &S + 347..348 'S': S + "#]], ); } @@ -717,49 +703,41 @@ fn coerce_unsize_trait_object_to_trait_object() { // 602..606 'obj2': &dyn Baz check_infer_with_mismatches( r#" - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl, U> CoerceUnsized<&U> for &T {} - - trait Foo {} - trait Bar: Foo {} - trait Baz: Bar {} +//- minicore: coerce_unsized +trait Foo {} +trait Bar: Foo {} +trait Baz: Bar {} - struct S; - impl Foo for S {} - impl Bar for S {} - impl Baz for S {} +struct S; +impl Foo for S {} +impl Bar for S {} +impl Baz for S {} - fn test() { - let obj: &dyn Baz = &S; - let obj: &dyn Bar<_, _, _> = obj; - let obj: &dyn Foo<_, _> = obj; - let obj2: &dyn Baz = &S; - let _: &dyn Foo<_, _> = obj2; - } - "#, +fn test() { + let obj: &dyn Baz = &S; + let obj: &dyn Bar<_, _, _> = obj; + let obj: &dyn Foo<_, _> = obj; + let obj2: &dyn Baz = &S; + let _: &dyn Foo<_, _> = obj2; +} +"#, expect![[r#" - 424..609 '{ ...bj2; }': () - 434..437 'obj': &dyn Baz - 459..461 '&S': &S - 460..461 'S': S - 471..474 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> - 496..499 'obj': &dyn Baz - 509..512 'obj': &dyn Foo<{unknown}, {unknown}> - 531..534 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> - 544..548 'obj2': &dyn Baz - 570..572 '&S': &S - 571..572 'S': S - 582..583 '_': &dyn Foo<{unknown}, {unknown}> - 602..606 'obj2': &dyn Baz - 496..499: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz - 531..534: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}> - 602..606: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz + 236..421 '{ ...bj2; }': () + 246..249 'obj': &dyn Baz + 271..273 '&S': &S + 272..273 'S': S + 283..286 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> + 308..311 'obj': &dyn Baz + 321..324 'obj': &dyn Foo<{unknown}, {unknown}> + 343..346 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> + 356..360 'obj2': &dyn Baz + 382..384 '&S': &S + 383..384 'S': S + 394..395 '_': &dyn Foo<{unknown}, {unknown}> + 414..418 'obj2': &dyn Baz + 308..311: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz + 343..346: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}> + 414..418: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz "#]], ); } @@ -768,40 +746,32 @@ fn coerce_unsize_trait_object_to_trait_object() { fn coerce_unsize_super_trait_cycle() { check_infer_with_mismatches( r#" - #[lang = "sized"] - pub trait Sized {} - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl, U> CoerceUnsized<&U> for &T {} - - trait A {} - trait B: C + A {} - trait C: B {} - trait D: C - - struct S; - impl A for S {} - impl B for S {} - impl C for S {} - impl D for S {} +//- minicore: coerce_unsized +trait A {} +trait B: C + A {} +trait C: B {} +trait D: C - fn test() { - let obj: &dyn D = &S; - let obj: &dyn A = &S; - } - "#, - expect![[r" - 328..383 '{ ... &S; }': () - 338..341 'obj': &dyn D - 352..354 '&S': &S - 353..354 'S': S - 364..367 'obj': &dyn A - 378..380 '&S': &S - 379..380 'S': S - "]], +struct S; +impl A for S {} +impl B for S {} +impl C for S {} +impl D for S {} + +fn test() { + let obj: &dyn D = &S; + let obj: &dyn A = &S; +} +"#, + expect![[r#" + 140..195 '{ ... &S; }': () + 150..153 'obj': &dyn D + 164..166 '&S': &S + 165..166 'S': S + 176..179 'obj': &dyn A + 190..192 '&S': &S + 191..192 'S': S + "#]], ); } @@ -810,41 +780,35 @@ fn coerce_unsize_generic() { // FIXME: fix the type mismatches here check_infer_with_mismatches( r#" - #[lang = "unsize"] - pub trait Unsize {} - #[lang = "coerce_unsized"] - pub trait CoerceUnsized {} - - impl, U> CoerceUnsized<&U> for &T {} - - struct Foo { t: T }; - struct Bar(Foo); +//- minicore: coerce_unsized +struct Foo { t: T }; +struct Bar(Foo); - fn test() { - let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; - let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); - } - "#, +fn test() { + let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; + let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); +} +"#, expect![[r#" - 209..317 '{ ... }); }': () - 219..220 '_': &Foo<[usize]> - 238..259 '&Foo {..., 3] }': &Foo<[usize]> - 239..259 'Foo { ..., 3] }': Foo<[usize]> - 248..257 '[1, 2, 3]': [usize; 3] - 249..250 '1': usize - 252..253 '2': usize - 255..256 '3': usize - 269..270 '_': &Bar<[usize]> - 288..314 '&Bar(F... 3] })': &Bar<[i32; 3]> - 289..292 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]> - 289..314 'Bar(Fo... 3] })': Bar<[i32; 3]> - 293..313 'Foo { ..., 3] }': Foo<[i32; 3]> - 302..311 '[1, 2, 3]': [i32; 3] - 303..304 '1': i32 - 306..307 '2': i32 - 309..310 '3': i32 - 248..257: expected [usize], got [usize; 3] - 288..314: expected &Bar<[usize]>, got &Bar<[i32; 3]> + 58..166 '{ ... }); }': () + 68..69 '_': &Foo<[usize]> + 87..108 '&Foo {..., 3] }': &Foo<[usize]> + 88..108 'Foo { ..., 3] }': Foo<[usize]> + 97..106 '[1, 2, 3]': [usize; 3] + 98..99 '1': usize + 101..102 '2': usize + 104..105 '3': usize + 118..119 '_': &Bar<[usize]> + 137..163 '&Bar(F... 3] })': &Bar<[i32; 3]> + 138..141 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]> + 138..163 'Bar(Fo... 3] })': Bar<[i32; 3]> + 142..162 'Foo { ..., 3] }': Foo<[i32; 3]> + 151..160 '[1, 2, 3]': [i32; 3] + 152..153 '1': i32 + 155..156 '2': i32 + 158..159 '3': i32 + 97..106: expected [usize], got [usize; 3] + 137..163: expected &Bar<[usize]>, got &Bar<[i32; 3]> "#]], ); } @@ -854,15 +818,7 @@ fn coerce_unsize_apit() { // FIXME: #8984 check_infer_with_mismatches( r#" -#[lang = "sized"] -pub trait Sized {} -#[lang = "unsize"] -pub trait Unsize {} -#[lang = "coerce_unsized"] -pub trait CoerceUnsized {} - -impl, U> CoerceUnsized<&U> for &T {} - +//- minicore: coerce_unsized trait Foo {} fn test(f: impl Foo) { @@ -870,12 +826,12 @@ fn test(f: impl Foo) { } "#, expect![[r#" - 210..211 'f': impl Foo - 223..252 '{ ... &f; }': () - 233..234 '_': &dyn Foo - 247..249 '&f': &impl Foo - 248..249 'f': impl Foo - 247..249: expected &dyn Foo, got &impl Foo + 22..23 'f': impl Foo + 35..64 '{ ... &f; }': () + 45..46 '_': &dyn Foo + 59..61 '&f': &impl Foo + 60..61 'f': impl Foo + 59..61: expected &dyn Foo, got &impl Foo "#]], ); } @@ -971,15 +927,7 @@ fn main() { fn coerce_unsize_expected_type() { check_no_mismatches( r#" -#[lang = "sized"] -pub trait Sized {} -#[lang = "unsize"] -pub trait Unsize {} -#[lang = "coerce_unsized"] -pub trait CoerceUnsized {} - -impl, U> CoerceUnsized<&U> for &T {} - +//- minicore: coerce_unsized fn main() { let foo: &[u32] = &[1, 2]; let foo: &[u32] = match true { -- cgit v1.2.3 From 7ebac5e54c51c6b8f1ddf3bb905f625416cc09fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:34:26 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/coercion.rs | 65 ++++++++++++++-------------- crates/hir_ty/src/tests/method_resolution.rs | 44 ++++++++----------- crates/test_utils/src/minicore.rs | 28 ++++++++++-- 3 files changed, 75 insertions(+), 62 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index eca6ae1fe..713b74165 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -426,15 +426,15 @@ fn coerce_autoderef() { #[test] fn coerce_autoderef_generic() { check_infer_with_mismatches( - r" - struct Foo; - fn takes_ref(x: &T) -> T { *x } - fn test() { - takes_ref(&Foo); - takes_ref(&&Foo); - takes_ref(&&&Foo); - } - ", + r#" +struct Foo; +fn takes_ref(x: &T) -> T { *x } +fn test() { + takes_ref(&Foo); + takes_ref(&&Foo); + takes_ref(&&&Foo); +} +"#, expect![[r" 28..29 'x': &T 40..46 '{ *x }': T @@ -464,30 +464,29 @@ fn coerce_autoderef_generic() { fn coerce_autoderef_block() { check_infer_with_mismatches( r#" - struct String {} - #[lang = "deref"] - trait Deref { type Target; } - impl Deref for String { type Target = str; } - fn takes_ref_str(x: &str) {} - fn returns_string() -> String { loop {} } - fn test() { - takes_ref_str(&{ returns_string() }); - } - "#, - expect![[r" - 126..127 'x': &str - 135..137 '{}': () - 168..179 '{ loop {} }': String - 170..177 'loop {}': ! - 175..177 '{}': () - 190..235 '{ ... }); }': () - 196..209 'takes_ref_str': fn takes_ref_str(&str) - 196..232 'takes_...g() })': () - 210..231 '&{ ret...ng() }': &String - 211..231 '{ retu...ng() }': String - 213..227 'returns_string': fn returns_string() -> String - 213..229 'return...ring()': String - "]], +//- minicore: deref +struct String {} +impl core::ops::Deref for String { type Target = str; } +fn takes_ref_str(x: &str) {} +fn returns_string() -> String { loop {} } +fn test() { + takes_ref_str(&{ returns_string() }); +} +"#, + expect![[r#" + 90..91 'x': &str + 99..101 '{}': () + 132..143 '{ loop {} }': String + 134..141 'loop {}': ! + 139..141 '{}': () + 154..199 '{ ... }); }': () + 160..173 'takes_ref_str': fn takes_ref_str(&str) + 160..196 'takes_...g() })': () + 174..195 '&{ ret...ng() }': &String + 175..195 '{ retu...ng() }': String + 177..191 'returns_string': fn returns_string() -> String + 177..193 'return...ring()': String + "#]], ); } diff --git a/crates/hir_ty/src/tests/method_resolution.rs b/crates/hir_ty/src/tests/method_resolution.rs index f26b2c8a7..79108054c 100644 --- a/crates/hir_ty/src/tests/method_resolution.rs +++ b/crates/hir_ty/src/tests/method_resolution.rs @@ -780,10 +780,7 @@ fn test() { (&S).foo(); } fn method_resolution_unsize_array() { check_types( r#" -#[lang = "slice"] -impl [T] { - fn len(&self) -> usize { loop {} } -} +//- minicore: slice fn test() { let a = [1, 2, 3]; a.len(); @@ -1178,11 +1175,7 @@ fn main() { fn autoderef_visibility_field() { check_infer( r#" -#[lang = "deref"] -pub trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} +//- minicore: deref mod a { pub struct Foo(pub char); pub struct Bar(i32); @@ -1191,7 +1184,7 @@ mod a { Self(0) } } - impl super::Deref for Bar { + impl core::ops::Deref for Bar { type Target = Foo; fn deref(&self) -> &Foo { &Foo('z') @@ -1205,22 +1198,21 @@ mod b { } "#, expect![[r#" - 67..71 'self': &Self - 200..231 '{ ... }': Bar - 214..218 'Self': Bar(i32) -> Bar - 214..221 'Self(0)': Bar - 219..220 '0': i32 - 315..319 'self': &Bar - 329..362 '{ ... }': &Foo - 343..352 '&Foo('z')': &Foo - 344..347 'Foo': Foo(char) -> Foo - 344..352 'Foo('z')': Foo - 348..351 ''z'': char - 392..439 '{ ... }': () - 406..407 'x': char - 410..428 'super:...r::new': fn new() -> Bar - 410..430 'super:...:new()': Bar - 410..432 'super:...ew().0': char + 107..138 '{ ... }': Bar + 121..125 'Self': Bar(i32) -> Bar + 121..128 'Self(0)': Bar + 126..127 '0': i32 + 226..230 'self': &Bar + 240..273 '{ ... }': &Foo + 254..263 '&Foo('z')': &Foo + 255..258 'Foo': Foo(char) -> Foo + 255..263 'Foo('z')': Foo + 259..262 ''z'': char + 303..350 '{ ... }': () + 317..318 'x': char + 321..339 'super:...r::new': fn new() -> Bar + 321..341 'super:...:new()': Bar + 321..343 'super:...ew().0': char "#]], ) } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 8f8f1c9f8..a61459f6d 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -9,7 +9,9 @@ //! //! Available flags: //! sized: +//! slice: //! unsize: sized +//! deref: sized //! coerce_unsized: unsize pub mod marker { @@ -27,8 +29,8 @@ pub mod marker { } pub mod ops { + // region:coerce_unsized mod unsize { - // region:coerce_unsized use crate::marker::Unsize; #[lang = "coerce_unsized"] @@ -45,11 +47,31 @@ pub mod ops { impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} impl, U: ?Sized> CoerceUnsized<*const U> for *mut T {} impl, U: ?Sized> CoerceUnsized<*const U> for *const T {} - // endregion:coerce_unsized } + pub use self::unsize::CoerceUnsized; + // endregion:coerce_unsized - pub use self::unsize::CoerceUnsized; // :coerce_unsized + // region:deref + mod deref { + #[lang = "deref"] + pub trait Deref { + #[lang = "deref_target"] + type Target: ?Sized; + fn deref(&self) -> &Self::Target; + } + } + pub use self::deref::Deref; + // endregion:deref +} + +// region:slice +pub mod slice { + #[lang = "slice"] + impl [T] { + pub fn len(&self) -> usize { loop {} } + } } +// endregion:slice pub mod prelude { pub mod v1 { -- cgit v1.2.3 From b737b894cb8974361881d98d7d0a6f75839ca345 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:36:50 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/method_resolution.rs | 55 +++++----- crates/hir_ty/src/tests/simple.rs | 158 +++++++++++++-------------- 2 files changed, 102 insertions(+), 111 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/method_resolution.rs b/crates/hir_ty/src/tests/method_resolution.rs index 79108054c..d9b5ee9cf 100644 --- a/crates/hir_ty/src/tests/method_resolution.rs +++ b/crates/hir_ty/src/tests/method_resolution.rs @@ -1222,11 +1222,7 @@ fn autoderef_visibility_method() { cov_mark::check!(autoderef_candidate_not_visible); check_infer( r#" -#[lang = "deref"] -pub trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} +//- minicore: deref mod a { pub struct Foo(pub char); impl Foo { @@ -1243,7 +1239,7 @@ mod a { self.0 } } - impl super::Deref for Bar { + impl core::ops::Deref for Bar { type Target = Foo; fn deref(&self) -> &Foo { &Foo('z') @@ -1257,30 +1253,29 @@ mod b { } "#, expect![[r#" - 67..71 'self': &Self - 168..172 'self': &Foo - 182..212 '{ ... }': char - 196..200 'self': &Foo - 196..202 'self.0': char - 288..319 '{ ... }': Bar - 302..306 'Self': Bar(i32) -> Bar - 302..309 'Self(0)': Bar - 307..308 '0': i32 - 338..342 'self': &Bar - 351..381 '{ ... }': i32 - 365..369 'self': &Bar - 365..371 'self.0': i32 - 465..469 'self': &Bar - 479..512 '{ ... }': &Foo - 493..502 '&Foo('z')': &Foo - 494..497 'Foo': Foo(char) -> Foo - 494..502 'Foo('z')': Foo - 498..501 ''z'': char - 542..595 '{ ... }': () - 556..557 'x': char - 560..578 'super:...r::new': fn new() -> Bar - 560..580 'super:...:new()': Bar - 560..588 'super:...ango()': char + 75..79 'self': &Foo + 89..119 '{ ... }': char + 103..107 'self': &Foo + 103..109 'self.0': char + 195..226 '{ ... }': Bar + 209..213 'Self': Bar(i32) -> Bar + 209..216 'Self(0)': Bar + 214..215 '0': i32 + 245..249 'self': &Bar + 258..288 '{ ... }': i32 + 272..276 'self': &Bar + 272..278 'self.0': i32 + 376..380 'self': &Bar + 390..423 '{ ... }': &Foo + 404..413 '&Foo('z')': &Foo + 405..408 'Foo': Foo(char) -> Foo + 405..413 'Foo('z')': Foo + 409..412 ''z'': char + 453..506 '{ ... }': () + 467..468 'x': char + 471..489 'super:...r::new': fn new() -> Bar + 471..491 'super:...:new()': Bar + 471..499 'super:...ango()': char "#]], ) } diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 3418ed21e..81d0215cf 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -951,62 +951,57 @@ fn infer_argument_autoderef() { fn infer_method_argument_autoderef() { check_infer( r#" - #[lang = "deref"] - pub trait Deref { - type Target; - fn deref(&self) -> &Self::Target; - } +//- minicore: deref +use core::ops::Deref; +struct A(*mut T); - struct A(*mut T); +impl A { + fn foo(&self, x: &A) -> &T { + &*x.0 + } +} - impl A { - fn foo(&self, x: &A) -> &T { - &*x.0 - } - } +struct B(T); - struct B(T); - - impl Deref for B { - type Target = T; - fn deref(&self) -> &Self::Target { - &self.0 - } - } +impl Deref for B { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} - fn test(a: A) { - let t = A(0 as *mut _).foo(&&B(B(a))); - } - "#, +fn test(a: A) { + let t = A(0 as *mut _).foo(&&B(B(a))); +} +"#, expect![[r#" - 67..71 'self': &Self - 143..147 'self': &A - 149..150 'x': &A - 165..186 '{ ... }': &T - 175..180 '&*x.0': &T - 176..180 '*x.0': T - 177..178 'x': &A - 177..180 'x.0': *mut T - 267..271 'self': &B - 290..313 '{ ... }': &T - 300..307 '&self.0': &T - 301..305 'self': &B - 301..307 'self.0': T - 325..326 'a': A - 336..382 '{ ...))); }': () - 346..347 't': &i32 - 350..351 'A': A(*mut i32) -> A - 350..364 'A(0 as *mut _)': A - 350..379 'A(0 as...B(a)))': &i32 - 352..353 '0': i32 - 352..363 '0 as *mut _': *mut i32 - 369..378 '&&B(B(a))': &&B>> - 370..378 '&B(B(a))': &B>> - 371..372 'B': B>>(B>) -> B>> - 371..378 'B(B(a))': B>> - 373..374 'B': B>(A) -> B> - 373..377 'B(a)': B> - 375..376 'a': A + 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..108 'x.0': *mut T + 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 + 278..279 'A': A(*mut i32) -> A + 278..292 'A(0 as *mut _)': A + 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>> + 299..300 'B': B>>(B>) -> B>> + 299..306 'B(B(a))': B>> + 301..302 'B': B>(A) -> B> + 301..305 'B(a)': B> + 303..304 'a': A "#]], ); } @@ -1015,15 +1010,15 @@ fn infer_method_argument_autoderef() { fn infer_in_elseif() { check_infer( r#" - struct Foo { field: i32 } - fn main(foo: Foo) { - if true { +struct Foo { field: i32 } +fn main(foo: Foo) { + if true { - } else if false { - foo.field - } - } - "#, + } else if false { + foo.field + } +} +"#, expect![[r#" 34..37 'foo': Foo 44..108 '{ ... } }': () @@ -1043,28 +1038,29 @@ fn infer_in_elseif() { fn infer_if_match_with_return() { check_infer( r#" - fn foo() { - let _x1 = if true { - 1 - } else { - return; - }; - let _x2 = if true { - 2 - } else { - return - }; - let _x3 = match true { - true => 3, - _ => { - return; - } - }; - let _x4 = match true { - true => 4, - _ => return - }; - }"#, +fn foo() { + let _x1 = if true { + 1 + } else { + return; + }; + let _x2 = if true { + 2 + } else { + return + }; + let _x3 = match true { + true => 3, + _ => { + return; + } + }; + let _x4 = match true { + true => 4, + _ => return + }; +} +"#, expect![[r#" 9..322 '{ ... }; }': () 19..22 '_x1': i32 -- cgit v1.2.3 From f4b52682dad6dbf31fb17beb645e362e359ee119 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:39:44 +0300 Subject: internal: unindent some tests --- crates/hir_ty/src/tests/simple.rs | 507 +++++++++++++++++++------------------- 1 file changed, 252 insertions(+), 255 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 81d0215cf..2687c6a44 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -175,16 +175,17 @@ fn test() { fn infer_basics() { check_infer( r#" - fn test(a: u32, b: isize, c: !, d: &str) { - a; - b; - c; - d; - 1usize; - 1isize; - "test"; - 1.0f32; - }"#, +fn test(a: u32, b: isize, c: !, d: &str) { + a; + b; + c; + d; + 1usize; + 1isize; + "test"; + 1.0f32; +} +"#, expect![[r#" 8..9 'a': u32 16..17 'b': isize @@ -207,15 +208,15 @@ fn infer_basics() { fn infer_let() { check_infer( r#" - fn test() { - let a = 1isize; - let b: usize = 1; - let c = b; - let d: u32; - let e; - let f: i32 = e; - } - "#, +fn test() { + let a = 1isize; + let b: usize = 1; + let c = b; + let d: u32; + let e; + let f: i32 = e; +} +"#, expect![[r#" 10..117 '{ ...= e; }': () 20..21 'a': isize @@ -236,17 +237,17 @@ fn infer_let() { fn infer_paths() { check_infer( r#" - fn a() -> u32 { 1 } +fn a() -> u32 { 1 } - mod b { - fn c() -> u32 { 1 } - } +mod b { + fn c() -> u32 { 1 } +} - fn test() { - a(); - b::c(); - } - "#, +fn test() { + a(); + b::c(); +} +"#, expect![[r#" 14..19 '{ 1 }': u32 16..17 '1': u32 @@ -265,17 +266,17 @@ fn infer_paths() { fn infer_path_type() { check_infer( r#" - struct S; +struct S; - impl S { - fn foo() -> i32 { 1 } - } +impl S { + fn foo() -> i32 { 1 } +} - fn test() { - S::foo(); - ::foo(); - } - "#, +fn test() { + S::foo(); + ::foo(); +} +"#, expect![[r#" 40..45 '{ 1 }': i32 42..43 '1': i32 @@ -292,21 +293,21 @@ fn infer_path_type() { fn infer_struct() { check_infer( r#" - struct A { - b: B, - c: C, - } - struct B; - struct C(usize); +struct A { + b: B, + c: C, +} +struct B; +struct C(usize); - fn test() { - let c = C(1); - B; - let a: A = A { b: B, c: C(1) }; - a.b; - a.c; - } - "#, +fn test() { + let c = C(1); + B; + let a: A = A { b: B, c: C(1) }; + a.b; + a.c; +} +"#, expect![[r#" 71..153 '{ ...a.c; }': () 81..82 'c': C @@ -332,14 +333,15 @@ fn infer_struct() { fn infer_enum() { check_infer( r#" - enum E { - V1 { field: u32 }, - V2 - } - fn test() { - E::V1 { field: 1 }; - E::V2; - }"#, +enum E { + V1 { field: u32 }, + V2 +} +fn test() { + E::V1 { field: 1 }; + E::V2; +} +"#, expect![[r#" 51..89 '{ ...:V2; }': () 57..75 'E::V1 ...d: 1 }': E @@ -353,23 +355,23 @@ fn infer_enum() { fn infer_union() { check_infer( r#" - union MyUnion { - foo: u32, - bar: f32, - } +union MyUnion { + foo: u32, + bar: f32, +} - fn test() { - let u = MyUnion { foo: 0 }; - unsafe { baz(u); } - let u = MyUnion { bar: 0.0 }; - unsafe { baz(u); } - } +fn test() { + let u = MyUnion { foo: 0 }; + unsafe { baz(u); } + let u = MyUnion { bar: 0.0 }; + unsafe { baz(u); } +} - unsafe fn baz(u: MyUnion) { - let inner = u.foo; - let inner = u.bar; - } - "#, +unsafe fn baz(u: MyUnion) { + let inner = u.foo; + let inner = u.bar; +} +"#, expect![[r#" 57..172 '{ ...); } }': () 67..68 'u': MyUnion @@ -404,19 +406,19 @@ fn infer_union() { fn infer_refs() { check_infer( r#" - fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { - a; - *a; - &a; - &mut a; - b; - *b; - &b; - c; - *c; - d; - *d; - } +fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { + a; + *a; + &a; + &mut a; + b; + *b; + &b; + c; + *c; + d; + *d; +} "#, expect![[r#" 8..9 'a': &u32 @@ -450,11 +452,11 @@ fn infer_refs() { fn infer_raw_ref() { check_infer( r#" - fn test(a: i32) { - &raw mut a; - &raw const a; - } - "#, +fn test(a: i32) { + &raw mut a; + &raw const a; +} +"#, expect![[r#" 8..9 'a': i32 16..53 '{ ...t a; }': () @@ -524,26 +526,26 @@ h"; fn infer_unary_op() { check_infer( r#" - enum SomeType {} - - fn test(x: SomeType) { - let b = false; - let c = !b; - let a = 100; - let d: i128 = -a; - let e = -100; - let f = !!!true; - let g = !42; - let h = !10u32; - let j = !a; - -3.14; - !3; - -x; - !x; - -"hello"; - !"hello"; - } - "#, +enum SomeType {} + +fn test(x: SomeType) { + let b = false; + let c = !b; + let a = 100; + let d: i128 = -a; + let e = -100; + let f = !!!true; + let g = !42; + let h = !10u32; + let j = !a; + -3.14; + !3; + -x; + !x; + -"hello"; + !"hello"; +} +"#, expect![[r#" 26..27 'x': SomeType 39..271 '{ ...lo"; }': () @@ -594,19 +596,19 @@ fn infer_unary_op() { fn infer_backwards() { check_infer( r#" - fn takes_u32(x: u32) {} +fn takes_u32(x: u32) {} - struct S { i32_field: i32 } +struct S { i32_field: i32 } - fn test() -> &mut &f64 { - let a = unknown_function(); - takes_u32(a); - let b = unknown_function(); - S { i32_field: b }; - let c = unknown_function(); - &mut &c - } - "#, +fn test() -> &mut &f64 { + let a = unknown_function(); + takes_u32(a); + let b = unknown_function(); + S { i32_field: b }; + let c = unknown_function(); + &mut &c +} +"#, expect![[r#" 13..14 'x': u32 21..23 '{}': () @@ -636,23 +638,23 @@ fn infer_backwards() { fn infer_self() { check_infer( r#" - struct S; +struct S; - impl S { - fn test(&self) { - self; - } - fn test2(self: &Self) { - self; - } - fn test3() -> Self { - S {} - } - fn test4() -> Self { - Self {} - } - } - "#, +impl S { + fn test(&self) { + self; + } + fn test2(self: &Self) { + self; + } + fn test3() -> Self { + S {} + } + fn test4() -> Self { + Self {} + } +} +"#, expect![[r#" 33..37 'self': &S 39..60 '{ ... }': () @@ -672,30 +674,30 @@ fn infer_self() { fn infer_self_as_path() { check_infer( r#" - struct S1; - struct S2(isize); - enum E { - V1, - V2(u32), - } +struct S1; +struct S2(isize); +enum E { + V1, + V2(u32), +} - impl S1 { - fn test() { - Self; - } - } - impl S2 { - fn test() { - Self(1); - } - } - impl E { - fn test() { - Self::V1; - Self::V2(1); - } - } - "#, +impl S1 { + fn test() { + Self; + } +} +impl S2 { + fn test() { + Self(1); + } +} +impl E { + fn test() { + Self::V1; + Self::V2(1); + } +} +"#, expect![[r#" 86..107 '{ ... }': () 96..100 'Self': S1 @@ -716,26 +718,26 @@ fn infer_self_as_path() { fn infer_binary_op() { check_infer( r#" - fn f(x: bool) -> i32 { - 0i32 - } +fn f(x: bool) -> i32 { + 0i32 +} - fn test() -> bool { - let x = a && b; - let y = true || false; - let z = x == y; - let t = x != y; - let minus_forty: isize = -40isize; - let h = minus_forty <= CONST_2; - let c = f(z || y) + 5; - let d = b; - let g = minus_forty ^= i; - let ten: usize = 10; - let ten_is_eleven = ten == some_num; - - ten < 3 - } - "#, +fn test() -> bool { + let x = a && b; + let y = true || false; + let z = x == y; + let t = x != y; + let minus_forty: isize = -40isize; + let h = minus_forty <= CONST_2; + let c = f(z || y) + 5; + let d = b; + let g = minus_forty ^= i; + let ten: usize = 10; + let ten_is_eleven = ten == some_num; + + ten < 3 +} +"#, expect![[r#" 5..6 'x': bool 21..33 '{ 0i32 }': i32 @@ -795,11 +797,11 @@ fn infer_binary_op() { fn infer_shift_op() { check_infer( r#" - fn test() { - 1u32 << 5u8; - 1u32 >> 5u8; - } - "#, +fn test() { + 1u32 << 5u8; + 1u32 >> 5u8; +} +"#, expect![[r#" 10..47 '{ ...5u8; }': () 16..20 '1u32': u32 @@ -816,29 +818,29 @@ fn infer_shift_op() { fn infer_field_autoderef() { check_infer( r#" - struct A { - b: B, - } - struct B; - - fn test1(a: A) { - let a1 = a; - a1.b; - let a2 = &a; - a2.b; - let a3 = &mut a; - a3.b; - let a4 = &&&&&&&a; - a4.b; - let a5 = &mut &&mut &&mut a; - a5.b; - } +struct A { + b: B, +} +struct B; - fn test2(a1: *const A, a2: *mut A) { - a1.b; - a2.b; - } - "#, +fn test1(a: A) { + let a1 = a; + a1.b; + let a2 = &a; + a2.b; + let a3 = &mut a; + a3.b; + let a4 = &&&&&&&a; + a4.b; + let a5 = &mut &&mut &&mut a; + a5.b; +} + +fn test2(a1: *const A, a2: *mut A) { + a1.b; + a2.b; +} +"#, expect![[r#" 43..44 'a': A 49..212 '{ ...5.b; }': () @@ -891,58 +893,53 @@ fn infer_field_autoderef() { fn infer_argument_autoderef() { check_infer( r#" - #[lang = "deref"] - pub trait Deref { - type Target; - fn deref(&self) -> &Self::Target; - } - - struct A(T); +//- minicore: deref +use core::ops::Deref; +struct A(T); - impl A { - fn foo(&self) -> &T { - &self.0 - } - } +impl A { + fn foo(&self) -> &T { + &self.0 + } +} - struct B(T); +struct B(T); - impl Deref for B { - type Target = T; - fn deref(&self) -> &Self::Target { - &self.0 - } - } +impl Deref for B { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} - fn test() { - let t = A::foo(&&B(B(A(42)))); - } - "#, +fn test() { + let t = A::foo(&&B(B(A(42)))); +} +"#, expect![[r#" - 67..71 'self': &Self - 138..142 'self': &A - 150..173 '{ ... }': &T - 160..167 '&self.0': &T - 161..165 'self': &A - 161..167 'self.0': T - 254..258 'self': &B - 277..300 '{ ... }': &T - 287..294 '&self.0': &T - 288..292 'self': &B - 288..294 'self.0': T - 314..352 '{ ...))); }': () - 324..325 't': &i32 - 328..334 'A::foo': fn foo(&A) -> &i32 - 328..349 'A::foo...42))))': &i32 - 335..348 '&&B(B(A(42)))': &&B>> - 336..348 '&B(B(A(42)))': &B>> - 337..338 'B': B>>(B>) -> B>> - 337..348 'B(B(A(42)))': B>> - 339..340 'B': B>(A) -> B> - 339..347 'B(A(42))': B> - 341..342 'A': A(i32) -> A - 341..346 'A(42)': A - 343..345 '42': i32 + 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 + 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>> + 265..266 'B': B>>(B>) -> B>> + 265..276 'B(B(A(42)))': B>> + 267..268 'B': B>(A) -> B> + 267..275 'B(A(42))': B> + 269..270 'A': A(i32) -> A + 269..274 'A(42)': A + 271..273 '42': i32 "#]], ); } -- cgit v1.2.3 From 0bb1f1bc90ee0f0f92f55823fc2e0c12c6acb680 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:45:25 +0300 Subject: internal: add ranges to minicore --- crates/hir_ty/src/tests/simple.rs | 28 +------------------------- crates/test_utils/src/minicore.rs | 42 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 2687c6a44..b63cda912 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -113,7 +113,7 @@ fn type_alias_in_struct_lit() { fn infer_ranges() { check_types( r#" -//- /main.rs crate:main deps:core +//- minicore: range fn test() { let a = ..; let b = 1..; @@ -125,32 +125,6 @@ fn test() { let t = (a, b, c, d, e, f); t; } //^ (RangeFull, RangeFrom, RangeTo, Range, RangeToInclusive, RangeInclusive) - -//- /core.rs crate:core -#[prelude_import] use prelude::*; -mod prelude {} - -pub mod ops { - pub struct Range { - pub start: Idx, - pub end: Idx, - } - pub struct RangeFrom { - pub start: Idx, - } - struct RangeFull; - pub struct RangeInclusive { - start: Idx, - end: Idx, - is_empty: u8, - } - pub struct RangeTo { - pub end: Idx, - } - pub struct RangeToInclusive { - pub end: Idx, - } -} "#, ); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index a61459f6d..f9f14b7df 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -10,6 +10,7 @@ //! Available flags: //! sized: //! slice: +//! range: //! unsize: sized //! deref: sized //! coerce_unsized: unsize @@ -62,13 +63,52 @@ pub mod ops { } pub use self::deref::Deref; // endregion:deref + + //region:range + mod range { + #[lang = "RangeFull"] + pub struct RangeFull; + + #[lang = "Range"] + pub struct Range { + pub start: Idx, + pub end: Idx, + } + + #[lang = "RangeFrom"] + pub struct RangeFrom { + pub start: Idx, + } + + #[lang = "RangeTo"] + pub struct RangeTo { + pub end: Idx, + } + + #[lang = "RangeInclusive"] + pub struct RangeInclusive { + pub(crate) start: Idx, + pub(crate) end: Idx, + pub(crate) exhausted: bool, + } + + #[lang = "RangeToInclusive"] + pub struct RangeToInclusive { + pub end: Idx, + } + } + pub use self::range::{Range, RangeFrom, RangeFull, RangeTo}; + pub use self::range::{RangeInclusive, RangeToInclusive}; + //endregion:range } // region:slice pub mod slice { #[lang = "slice"] impl [T] { - pub fn len(&self) -> usize { loop {} } + pub fn len(&self) -> usize { + loop {} + } } } // endregion:slice -- cgit v1.2.3 From ee13e895e337d01ea283a94b7896fddc0a922cb8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 21:57:56 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/regression.rs | 50 +++++++++++++++++------------------ crates/hir_ty/src/tests/traits.rs | 37 ++++++-------------------- 2 files changed, 32 insertions(+), 55 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index 1019e783b..1e0233b55 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs @@ -927,35 +927,33 @@ fn issue_6628() { fn issue_6852() { check_infer( r#" - #[lang = "deref"] - pub trait Deref { - type Target; - } +//- minicore: deref +use core::ops::Deref; - struct BufWriter {} +struct BufWriter {} - struct Mutex {} - struct MutexGuard<'a, T> {} - impl Mutex { - fn lock(&self) -> MutexGuard<'_, T> {} - } - impl<'a, T: 'a> Deref for MutexGuard<'a, T> { - type Target = T; - } - fn flush(&self) { - let w: &Mutex; - *(w.lock()); - } - "#, +struct Mutex {} +struct MutexGuard<'a, T> {} +impl Mutex { + fn lock(&self) -> MutexGuard<'_, T> {} +} +impl<'a, T: 'a> Deref for MutexGuard<'a, T> { + type Target = T; +} +fn flush(&self) { + let w: &Mutex; + *(w.lock()); +} +"#, expect![[r#" - 156..160 'self': &Mutex - 183..185 '{}': () - 267..271 'self': &{unknown} - 273..323 '{ ...()); }': () - 283..284 'w': &Mutex - 309..320 '*(w.lock())': BufWriter - 311..312 'w': &Mutex - 311..319 'w.lock()': MutexGuard + 123..127 'self': &Mutex + 150..152 '{}': () + 234..238 'self': &{unknown} + 240..290 '{ ...()); }': () + 250..251 'w': &Mutex + 276..287 '*(w.lock())': BufWriter + 278..279 'w': &Mutex + 278..286 'w.lock()': MutexGuard "#]], ); } diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index c830e576e..d237c3998 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -704,14 +704,9 @@ mod ops { fn deref_trait() { check_types( r#" -#[lang = "deref"] -trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} - +//- minicore: deref struct Arc; -impl Deref for Arc { +impl core::ops::Deref for Arc { type Target = T; } @@ -731,16 +726,10 @@ fn test(s: Arc) { fn deref_trait_with_inference_var() { check_types( r#" -//- /main.rs -#[lang = "deref"] -trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} - +//- minicore: deref struct Arc; fn new_arc() -> Arc {} -impl Deref for Arc { +impl core::ops::Deref for Arc { type Target = T; } @@ -761,15 +750,10 @@ fn test() { fn deref_trait_infinite_recursion() { check_types( r#" -#[lang = "deref"] -trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} - +//- minicore: deref struct S; -impl Deref for S { +impl core::ops::Deref for S { type Target = S; } @@ -784,14 +768,9 @@ fn test(s: S) { fn deref_trait_with_question_mark_size() { check_types( r#" -#[lang = "deref"] -trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} - +//- minicore: deref struct Arc; -impl Deref for Arc { +impl core::ops::Deref for Arc { type Target = T; } -- cgit v1.2.3 From 09c4013ec01b5eafc436cb9369a81036297379c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 22:02:29 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/regression.rs | 48 +++++++++++------------ crates/hir_ty/src/tests/simple.rs | 74 +++++++++++++++++------------------ crates/hir_ty/src/tests/traits.rs | 7 +--- 3 files changed, 59 insertions(+), 70 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index 1e0233b55..abd9c385a 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs @@ -975,37 +975,33 @@ fn param_overrides_fn() { fn lifetime_from_chalk_during_deref() { check_types( r#" - #[lang = "deref"] - pub trait Deref { - type Target; - } - - struct Box {} - impl Deref for Box { - type Target = T; +//- minicore: deref +struct Box {} +impl core::ops::Deref for Box { + type Target = T; - fn deref(&self) -> &Self::Target { - loop {} - } - } + fn deref(&self) -> &Self::Target { + loop {} + } +} - trait Iterator { - type Item; - } +trait Iterator { + type Item; +} - pub struct Iter<'a, T: 'a> { - inner: Box + 'a>, - } +pub struct Iter<'a, T: 'a> { + inner: Box + 'a>, +} - trait IterTrait<'a, T: 'a>: Iterator { - fn clone_box(&self); - } +trait IterTrait<'a, T: 'a>: Iterator { + fn clone_box(&self); +} - fn clone_iter(s: Iter) { - s.inner.clone_box(); - //^^^^^^^^^^^^^^^^^^^ () - } - "#, +fn clone_iter(s: Iter) { + s.inner.clone_box(); + //^^^^^^^^^^^^^^^^^^^ () +} +"#, ) } diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index b63cda912..68776f3c0 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -2606,11 +2606,8 @@ fn f() { fn infer_boxed_self_receiver() { check_infer( r#" -#[lang = "deref"] -pub trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} +//- minicore: deref +use core::ops::Deref; struct Box(T); @@ -2642,40 +2639,39 @@ fn main() { } "#, expect![[r#" - 67..71 'self': &Self - 175..179 'self': &Box - 259..263 'self': &Box> - 289..291 '{}': () - 313..317 'self': &Box> - 346..348 '{}': () - 368..372 'self': Box> - 393..395 '{}': () - 409..630 '{ ...r(); }': () - 419..424 'boxed': Box> - 427..430 'Box': Box>(Foo) -> Box> - 427..442 'Box(Foo(0_i32))': Box> - 431..434 'Foo': Foo(i32) -> Foo - 431..441 'Foo(0_i32)': Foo - 435..440 '0_i32': i32 - 453..457 'bad1': &i32 - 460..465 'boxed': Box> - 460..477 'boxed....nner()': &i32 - 487..492 'good1': &i32 - 495..509 'Foo::get_inner': fn get_inner(&Box>) -> &i32 - 495..517 'Foo::g...boxed)': &i32 - 510..516 '&boxed': &Box> - 511..516 'boxed': Box> - 528..532 'bad2': &Foo - 535..540 'boxed': Box> - 535..551 'boxed....self()': &Foo - 561..566 'good2': &Foo - 569..582 'Foo::get_self': fn get_self(&Box>) -> &Foo - 569..590 'Foo::g...boxed)': &Foo - 583..589 '&boxed': &Box> - 584..589 'boxed': Box> - 601..606 'inner': Foo - 609..614 'boxed': Box> - 609..627 'boxed....nner()': Foo + 104..108 'self': &Box + 188..192 'self': &Box> + 218..220 '{}': () + 242..246 'self': &Box> + 275..277 '{}': () + 297..301 'self': Box> + 322..324 '{}': () + 338..559 '{ ...r(); }': () + 348..353 'boxed': Box> + 356..359 'Box': Box>(Foo) -> Box> + 356..371 'Box(Foo(0_i32))': Box> + 360..363 'Foo': Foo(i32) -> Foo + 360..370 'Foo(0_i32)': Foo + 364..369 '0_i32': 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> + 440..445 'boxed': Box> + 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> + 513..518 'boxed': Box> + 530..535 'inner': Foo + 538..543 'boxed': Box> + 538..556 'boxed....nner()': Foo "#]], ); } diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index d237c3998..fb13e3ac5 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -2604,12 +2604,9 @@ fn test() { fn dyn_trait_through_chalk() { check_types( r#" +//- minicore: deref struct Box {} -#[lang = "deref"] -trait Deref { - type Target; -} -impl Deref for Box { +impl core::ops::Deref for Box { type Target = T; } trait Trait { -- cgit v1.2.3 From 2eef66a2ed9b02962511e38c620efb23e2d65b00 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 22:38:21 +0300 Subject: internal: sanity-check minicore flags --- crates/test_utils/src/fixture.rs | 9 +++++++++ crates/test_utils/src/minicore.rs | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index 535892f3f..779146084 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -276,6 +276,7 @@ impl MiniCore { } let mut curr_region = ""; + let mut seen_regions = Vec::new(); for line in lines { let trimmed = line.trim(); if let Some(region) = trimmed.strip_prefix("// region:") { @@ -288,6 +289,7 @@ impl MiniCore { curr_region = ""; continue; } + seen_regions.push(curr_region); let mut flag = curr_region; if let Some(idx) = trimmed.find("// :") { @@ -305,6 +307,13 @@ impl MiniCore { buf.push_str(line) } } + + for flag in &self.valid_flags { + if !seen_regions.iter().any(|it| it == flag) { + panic!("unused minicore flag: {:?}", flag); + } + } + buf } } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index f9f14b7df..8c0d122dc 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -64,7 +64,7 @@ pub mod ops { pub use self::deref::Deref; // endregion:deref - //region:range + // region:range mod range { #[lang = "RangeFull"] pub struct RangeFull; @@ -99,7 +99,7 @@ pub mod ops { } pub use self::range::{Range, RangeFrom, RangeFull, RangeTo}; pub use self::range::{RangeInclusive, RangeToInclusive}; - //endregion:range + // endregion:range } // region:slice -- cgit v1.2.3 From 3efe5c3426a311b6d617f9718b82e9a598dfa06d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 22:49:00 +0300 Subject: internal: add future to minicore --- crates/hir_ty/src/tests/traits.rs | 14 ++------------ crates/test_utils/src/minicore.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 12 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index fb13e3ac5..33689e081 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -6,10 +6,10 @@ use super::{check_infer, check_infer_with_mismatches, check_types}; fn infer_await() { check_types( r#" -//- /main.rs crate:main deps:core +//- minicore: future struct IntFuture; -impl Future for IntFuture { +impl core::future::Future for IntFuture { type Output = u64; } @@ -18,16 +18,6 @@ fn test() { let v = r.await; v; } //^ u64 - -//- /core.rs crate:core -pub mod prelude { - pub mod rust_2018 { - #[lang = "future_trait"] - pub trait Future { - type Output; - } - } -} "#, ); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 8c0d122dc..127d06e59 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -14,6 +14,8 @@ //! unsize: sized //! deref: sized //! coerce_unsized: unsize +//! pin: +//! future: pin pub mod marker { // region:sized @@ -113,6 +115,41 @@ pub mod slice { } // endregion:slice +// region:pin +pub mod pin { + #[lang = "pin"] + #[fundamental] + pub struct Pin

{ + pointer: P, + } +} +// endregion:pin + +// region:future +pub mod future { + use crate::{pin::Pin, task::{Poll, Context}}; + + #[lang = "future_trait"] + pub trait Future { + type Output; + #[lang = "poll"] + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll; + } +} +pub mod task { + pub enum Poll { + #[lang = "Ready"] + Ready(T), + #[lang = "Pending"] + Pending, + } + + pub struct Context<'a> { + waker: &'a (), + } +} +// endregion:future + pub mod prelude { pub mod v1 { pub use crate::marker::Sized; // :sized -- cgit v1.2.3 From 2870d2bade94ae276cba56caf8c35feaacd49422 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 22:59:51 +0300 Subject: internal: add option to minicore --- crates/hir_ty/src/tests/traits.rs | 46 +++++---------------------------------- crates/test_utils/src/fixture.rs | 2 ++ crates/test_utils/src/minicore.rs | 18 ++++++++++++++- 3 files changed, 25 insertions(+), 41 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 33689e081..6df8181ed 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -26,25 +26,14 @@ fn test() { fn infer_async() { check_types( r#" -//- /main.rs crate:main deps:core -async fn foo() -> u64 { - 128 -} +//- minicore: future +async fn foo() -> u64 { 128 } fn test() { let r = foo(); let v = r.await; v; } //^ u64 - -//- /core.rs crate:core -#[prelude_import] use future::*; -mod future { - #[lang = "future_trait"] - trait Future { - type Output; - } -} "#, ); } @@ -53,24 +42,13 @@ mod future { fn infer_desugar_async() { check_types( r#" -//- /main.rs crate:main deps:core -async fn foo() -> u64 { - 128 -} +//- minicore: future +async fn foo() -> u64 { 128 } fn test() { let r = foo(); r; } //^ impl Future - -//- /core.rs crate:core -#[prelude_import] use future::*; -mod future { - trait Future { - type Output; - } -} - "#, ); } @@ -79,7 +57,7 @@ mod future { fn infer_async_block() { check_types( r#" -//- /main.rs crate:main deps:core +//- minicore: future, option async fn test() { let a = async { 42 }; a; @@ -91,7 +69,7 @@ async fn test() { b; // ^ () let c = async { - let y = Option::None; + let y = None; y // ^ Option }; @@ -99,18 +77,6 @@ async fn test() { c; // ^ impl Future> } - -enum Option { None, Some(T) } - -//- /core.rs crate:core -#[prelude_import] use future::*; -mod future { - #[lang = "future_trait"] - trait Future { - type Output; - } -} - "#, ); } diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index 779146084..6ba112de8 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -198,6 +198,7 @@ impl MiniCore { self.activated_flags.iter().any(|it| it == flag) } + #[track_caller] fn assert_valid_flag(&self, flag: &str) { if !self.valid_flags.iter().any(|it| it == flag) { panic!("invalid flag: {:?}, valid flags: {:?}", flag, self.valid_flags); @@ -299,6 +300,7 @@ impl MiniCore { let skip = if flag == "" { false } else { + assert!(!flag.starts_with(' '), "region marker starts with a space: {:?}", flag); self.assert_valid_flag(flag); !self.has_flag(flag) }; diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 127d06e59..cb18c8796 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -16,6 +16,7 @@ //! coerce_unsized: unsize //! pin: //! future: pin +//! option: pub mod marker { // region:sized @@ -115,6 +116,17 @@ pub mod slice { } // endregion:slice +// region:option +pub mod option { + pub enum Option { + #[lang = "None"] + None, + #[lang = "Some"] + Some(T), + } +} +// endregion:option + // region:pin pub mod pin { #[lang = "pin"] @@ -127,7 +139,10 @@ pub mod pin { // region:future pub mod future { - use crate::{pin::Pin, task::{Poll, Context}}; + use crate::{ + pin::Pin, + task::{Context, Poll}, + }; #[lang = "future_trait"] pub trait Future { @@ -153,6 +168,7 @@ pub mod task { pub mod prelude { pub mod v1 { pub use crate::marker::Sized; // :sized + pub use crate::option::Option::{self, None, Some}; // :option } pub mod rust_2015 { -- cgit v1.2.3 From ae92057df6608f6ab48317198a9d13a97f07f004 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 23:02:38 +0300 Subject: internal: switch some tests to minicore --- crates/ide_completion/src/completions/keyword.rs | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'crates') diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index ba13d3707..0fccbeccf 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs @@ -536,17 +536,11 @@ Some multi-line comment$0 fn test_completion_await_impls_future() { check( r#" -//- /main.rs crate:main deps:std -use std::future::*; +//- minicore: future +use core::future::*; struct A {} impl Future for A {} fn foo(a: A) { a.$0 } - -//- /std/lib.rs crate:std -pub mod future { - #[lang = "future_trait"] - pub trait Future {} -} "#, expect![[r#" kw await expr.await @@ -555,20 +549,12 @@ pub mod future { check( r#" -//- /main.rs crate:main deps:std +//- minicore: future use std::future::*; fn foo() { let a = async {}; a.$0 } - -//- /std/lib.rs crate:std -pub mod future { - #[lang = "future_trait"] - pub trait Future { - type Output; - } -} "#, expect![[r#" kw await expr.await -- cgit v1.2.3 From 0798cce9e564774b663fd965aaf2ef7e5778cb3f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 23:07:25 +0300 Subject: internal: add result to minicore --- crates/hir_ty/src/tests/traits.rs | 20 +------------------- crates/test_utils/src/minicore.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 21 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 6df8181ed..65fed02d2 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -3627,16 +3627,7 @@ impl foo::Foo for u32 { fn infer_async_ret_type() { check_types( r#" -//- /main.rs crate:main deps:core - -enum Result { - Ok(T), - Err(E), -} - -use Result::*; - - +//- minicore: future, result struct Fooey; impl Fooey { @@ -3659,15 +3650,6 @@ async fn get_accounts() -> Result { // ^ u32 Ok(ret) } - -//- /core.rs crate:core -#[prelude_import] use future::*; -mod future { - #[lang = "future_trait"] - trait Future { - type Output; - } -} "#, ); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index cb18c8796..5ff60178c 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -17,6 +17,7 @@ //! pin: //! future: pin //! option: +//! result: pub mod marker { // region:sized @@ -127,6 +128,17 @@ pub mod option { } // endregion:option +// region:result +pub mod result { + pub enum Result { + #[lang = "Ok"] + Ok(T), + #[lang = "Err"] + Err(E), + } +} +// endregion:result + // region:pin pub mod pin { #[lang = "pin"] @@ -167,8 +179,11 @@ pub mod task { pub mod prelude { pub mod v1 { - pub use crate::marker::Sized; // :sized - pub use crate::option::Option::{self, None, Some}; // :option + pub use crate::{ + marker::Sized, // :sized + option::Option::{self, None, Some}, // :option + result::Result::{self, Err, Ok}, // :result + }; } pub mod rust_2015 { -- cgit v1.2.3 From 7cbcbccc7888b9a515cbebe2cd20fd3eebec463b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jun 2021 23:11:53 +0300 Subject: internal: switch some tests to minicore --- crates/ide/src/hover.rs | 15 +++++---------- crates/ide_completion/src/render.rs | 12 ++++-------- 2 files changed, 9 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index afeded315..14cf94d60 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3000,29 +3000,24 @@ fn foo(ar$0g: &impl Foo + Bar) {} fn test_hover_async_block_impl_trait_has_goto_type_action() { check_actions( r#" +//- minicore: future struct S; fn foo() { let fo$0o = async { S }; } - -#[prelude_import] use future::*; -mod future { - #[lang = "future_trait"] - pub trait Future { type Output; } -} "#, expect![[r#" [ GoToType( [ HoverGotoTypeData { - mod_path: "test::future::Future", + mod_path: "core::future::Future", nav: NavigationTarget { file_id: FileId( - 0, + 1, ), - full_range: 101..163, - focus_range: 140..146, + full_range: 244..426, + focus_range: 283..289, name: "Future", kind: Trait, description: "pub trait Future", diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index d8ca18c73..3eb51e80b 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -1152,16 +1152,11 @@ fn main() { fn suggest_deref() { check_relevance( r#" -#[lang = "deref"] -trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} - +//- minicore: deref struct S; struct T(S); -impl Deref for T { +impl core::ops::Deref for T { type Target = S; fn deref(&self) -> &Self::Target { @@ -1185,8 +1180,9 @@ fn main() { st T [] st S [] fn main() [] - tt Deref [] fn foo(…) [] + md core [] + tt Sized [] "#]], ) } -- cgit v1.2.3