Skip to content
Merged

1.52.0 #8718

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ide_completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl<'a> CompletionContext<'a> {
let def = self.sema.to_def(&it);
(def.map(|def| def.ret_type(self.db)), None)
},
ast::Stmt(it) => (None, None),
ast::Stmt(_it) => (None, None),
_ => {
match node.parent() {
Some(n) => {
Expand Down
7 changes: 3 additions & 4 deletions crates/ide_db/src/line_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::iter;

use rustc_hash::FxHashMap;
use stdx::partition_point;
use syntax::{TextRange, TextSize};

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -97,7 +96,7 @@ impl LineIndex {
}

pub fn line_col(&self, offset: TextSize) -> LineCol {
let line = partition_point(&self.newlines, |&it| it <= offset) - 1;
let line = self.newlines.partition_point(|&it| it <= offset) - 1;
let line_start_offset = self.newlines[line];
let col = offset - line_start_offset;
LineCol { line: line as u32, col: col.into() }
Expand All @@ -118,8 +117,8 @@ impl LineIndex {
}

pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
let lo = partition_point(&self.newlines, |&it| it < range.start());
let hi = partition_point(&self.newlines, |&it| it <= range.end());
let lo = self.newlines.partition_point(|&it| it < range.start());
let hi = self.newlines.partition_point(|&it| it <= range.end());
let all = iter::once(range.start())
.chain(self.newlines[lo..hi].iter().copied())
.chain(iter::once(range.end()));
Expand Down
3 changes: 1 addition & 2 deletions crates/project_model/src/cfg_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::str::FromStr;

use cfg::CfgOptions;
use stdx::split_once;

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum CfgFlag {
Expand All @@ -15,7 +14,7 @@ pub enum CfgFlag {
impl FromStr for CfgFlag {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match split_once(s, '=') {
let res = match s.split_once('=') {
Some((key, value)) => {
if !(value.starts_with('"') && value.ends_with('"')) {
return Err(format!("Invalid cfg ({:?}), value should be in quotes", s));
Expand Down
78 changes: 3 additions & 75 deletions crates/stdx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,6 @@ pub fn replace(buf: &mut String, from: char, to: &str) {
*buf = buf.replace(from, to)
}

// https://github.com/rust-lang/rust/issues/74773
pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
let mut split = haystack.splitn(2, delim);
let prefix = split.next()?;
let suffix = split.next()?;
Some((prefix, suffix))
}
pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
let mut split = haystack.rsplitn(2, delim);
let suffix = split.next()?;
let prefix = split.next()?;
Some((prefix, suffix))
}

pub fn trim_indent(mut text: &str) -> String {
if text.starts_with('\n') {
text = &text[1..];
Expand All @@ -89,7 +75,7 @@ pub fn trim_indent(mut text: &str) -> String {
.map(|it| it.len() - it.trim_start().len())
.min()
.unwrap_or(0);
lines_with_ends(text)
text.split_inclusive('\n')
.map(
|line| {
if line.len() <= indent {
Expand All @@ -102,70 +88,12 @@ pub fn trim_indent(mut text: &str) -> String {
.collect()
}

pub fn lines_with_ends(text: &str) -> LinesWithEnds {
LinesWithEnds { text }
}

pub struct LinesWithEnds<'a> {
text: &'a str,
}

impl<'a> Iterator for LinesWithEnds<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if self.text.is_empty() {
return None;
}
let idx = self.text.find('\n').map_or(self.text.len(), |it| it + 1);
let (res, next) = self.text.split_at(idx);
self.text = next;
Some(res)
}
}

/// Returns `idx` such that:
///
/// ```text
/// ∀ x in slice[..idx]: pred(x)
/// && ∀ x in slice[idx..]: !pred(x)
/// ```
///
/// https://github.com/rust-lang/rust/issues/73831
pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
where
P: FnMut(&T) -> bool,
{
let mut left = 0;
let mut right = slice.len();

while left != right {
let mid = left + (right - left) / 2;
// SAFETY:
// When left < right, left <= mid < right.
// Therefore left always increases and right always decreases,
// and either of them is selected.
// In both cases left <= right is satisfied.
// Therefore if left < right in a step,
// left <= right is satisfied in the next step.
// Therefore as long as left != right, 0 <= left < right <= len is satisfied
// and if this case 0 <= mid < len is satisfied too.
let value = unsafe { slice.get_unchecked(mid) };
if pred(value) {
left = mid + 1;
} else {
right = mid;
}
}

left
}

pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize>
where
F: FnMut(&T) -> Ordering,
{
let start = partition_point(slice, |it| key(it) == Ordering::Less);
let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal);
let start = slice.partition_point(|it| key(it) == Ordering::Less);
let len = slice[start..].partition_point(|it| key(it) == Ordering::Equal);
start..start + len
}

Expand Down
10 changes: 5 additions & 5 deletions crates/test_utils/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
//! ```
use rustc_hash::FxHashMap;
use stdx::{lines_with_ends, split_once, trim_indent};
use stdx::trim_indent;

#[derive(Debug, Eq, PartialEq)]
pub struct Fixture {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Fixture {

let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") };

for (ix, line) in default.into_iter().chain(lines_with_ends(&fixture)).enumerate() {
for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
if line.contains("//-") {
assert!(
line.starts_with("//-"),
Expand Down Expand Up @@ -133,22 +133,22 @@ impl Fixture {
let mut env = FxHashMap::default();
let mut introduce_new_source_root = false;
for component in components[1..].iter() {
let (key, value) = split_once(component, ':').unwrap();
let (key, value) = component.split_once(':').unwrap();
match key {
"crate" => krate = Some(value.to_string()),
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
"edition" => edition = Some(value.to_string()),
"cfg" => {
for entry in value.split(',') {
match split_once(entry, '=') {
match entry.split_once('=') {
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
None => cfg_atoms.push(entry.to_string()),
}
}
}
"env" => {
for key in value.split(',') {
if let Some((k, v)) = split_once(key, '=') {
if let Some((k, v)) = key.split_once('=') {
env.insert(k.into(), v.into());
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
};

use profile::StopWatch;
use stdx::{is_ci, lines_with_ends};
use stdx::is_ci;
use text_size::{TextRange, TextSize};

pub use dissimilar::diff as __diff;
Expand Down Expand Up @@ -181,7 +181,7 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
let mut prev_line_start: Option<TextSize> = None;
let mut line_start: TextSize = 0.into();
let mut prev_line_annotations: Vec<(TextSize, usize)> = Vec::new();
for line in lines_with_ends(text) {
for line in text.split_inclusive('\n') {
let mut this_line_annotations = Vec::new();
if let Some(idx) = line.find("//") {
let annotation_offset = TextSize::of(&line[..idx + "//".len()]);
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use xshell::{cmd, pushd};
use crate::flags;

// Latest stable, feel free to send a PR if this lags behind.
const REQUIRED_RUST_VERSION: u32 = 51;
const REQUIRED_RUST_VERSION: u32 = 52;

impl flags::Install {
pub(crate) fn run(self) -> Result<()> {
Expand Down