Move and document escape_literal function
This commit is contained in:
parent
e813b6d6e1
commit
aa33a6fca2
1 changed files with 22 additions and 19 deletions
|
@ -89,6 +89,28 @@ pub use need_type_info::TypeAnnotationNeeded;
|
||||||
|
|
||||||
pub mod nice_region_error;
|
pub mod nice_region_error;
|
||||||
|
|
||||||
|
/// Makes a valid string literal from a string by escaping special characters (" and \),
|
||||||
|
/// unless they are already escaped.
|
||||||
|
fn escape_literal(s: &str) -> String {
|
||||||
|
let mut escaped = String::with_capacity(s.len());
|
||||||
|
let mut chrs = s.chars().peekable();
|
||||||
|
while let Some(first) = chrs.next() {
|
||||||
|
match (first, chrs.peek()) {
|
||||||
|
('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
|
||||||
|
escaped.push('\\');
|
||||||
|
escaped.push(delim);
|
||||||
|
chrs.next();
|
||||||
|
}
|
||||||
|
('"' | '\'', _) => {
|
||||||
|
escaped.push('\\');
|
||||||
|
escaped.push(first)
|
||||||
|
}
|
||||||
|
(c, _) => escaped.push(c),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
escaped
|
||||||
|
}
|
||||||
|
|
||||||
/// A helper for building type related errors. The `typeck_results`
|
/// A helper for building type related errors. The `typeck_results`
|
||||||
/// field is only populated during an in-progress typeck.
|
/// field is only populated during an in-progress typeck.
|
||||||
/// Get an instance by calling `InferCtxt::err` or `FnCtxt::infer_err`.
|
/// Get an instance by calling `InferCtxt::err` or `FnCtxt::infer_err`.
|
||||||
|
@ -1904,25 +1926,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
terr: TypeError<'tcx>,
|
terr: TypeError<'tcx>,
|
||||||
) -> Vec<Error0308Subdiags> {
|
) -> Vec<Error0308Subdiags> {
|
||||||
use crate::traits::ObligationCauseCode::MatchExpressionArm;
|
use crate::traits::ObligationCauseCode::MatchExpressionArm;
|
||||||
fn escape_literal(s: &str) -> String {
|
|
||||||
let mut escaped = String::with_capacity(s.len());
|
|
||||||
let mut chrs = s.chars().peekable();
|
|
||||||
while let Some(first) = chrs.next() {
|
|
||||||
match (first, chrs.peek()) {
|
|
||||||
('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
|
|
||||||
escaped.push('\\');
|
|
||||||
escaped.push(delim);
|
|
||||||
chrs.next();
|
|
||||||
}
|
|
||||||
('"' | '\'', _) => {
|
|
||||||
escaped.push('\\');
|
|
||||||
escaped.push(first)
|
|
||||||
}
|
|
||||||
(c, _) => escaped.push(c),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
escaped
|
|
||||||
}
|
|
||||||
let mut suggestions = Vec::new();
|
let mut suggestions = Vec::new();
|
||||||
let span = trace.cause.span();
|
let span = trace.cause.span();
|
||||||
if let Some((expected, found)) = trace.values.ty() {
|
if let Some((expected, found)) = trace.values.ty() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue