Skip to content

Commit f9d20b6

Browse files
bors[bot]matklad
andauthored
Merge #9505
9505: internal: ensure consistent passing for config params r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 795b815 + 0db4f3f commit f9d20b6

File tree

6 files changed

+91
-60
lines changed

6 files changed

+91
-60
lines changed

crates/ide/src/annotations.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub struct AnnotationConfig {
4646

4747
pub(crate) fn annotations(
4848
db: &RootDatabase,
49+
config: &AnnotationConfig,
4950
file_id: FileId,
50-
config: AnnotationConfig,
5151
) -> Vec<Annotation> {
5252
let mut annotations = Vec::default();
5353

@@ -190,8 +190,7 @@ mod tests {
190190

191191
let annotations: Vec<Annotation> = analysis
192192
.annotations(
193-
file_id,
194-
AnnotationConfig {
193+
&AnnotationConfig {
195194
binary_target: true,
196195
annotate_runnables: true,
197196
annotate_impls: true,
@@ -200,6 +199,7 @@ mod tests {
200199
run: true,
201200
debug: true,
202201
},
202+
file_id,
203203
)
204204
.unwrap()
205205
.into_iter()

crates/ide/src/hover.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -532,27 +532,27 @@ mod tests {
532532

533533
fn check_hover_no_result(ra_fixture: &str) {
534534
let (analysis, position) = fixture::position(ra_fixture);
535-
assert!(analysis
535+
let hover = analysis
536536
.hover(
537-
position,
538537
&HoverConfig {
539538
links_in_hover: true,
540-
documentation: Some(HoverDocFormat::Markdown)
541-
}
539+
documentation: Some(HoverDocFormat::Markdown),
540+
},
541+
position,
542542
)
543-
.unwrap()
544-
.is_none());
543+
.unwrap();
544+
assert!(hover.is_none());
545545
}
546546

547547
fn check(ra_fixture: &str, expect: Expect) {
548548
let (analysis, position) = fixture::position(ra_fixture);
549549
let hover = analysis
550550
.hover(
551-
position,
552551
&HoverConfig {
553552
links_in_hover: true,
554553
documentation: Some(HoverDocFormat::Markdown),
555554
},
555+
position,
556556
)
557557
.unwrap()
558558
.unwrap();
@@ -568,11 +568,11 @@ mod tests {
568568
let (analysis, position) = fixture::position(ra_fixture);
569569
let hover = analysis
570570
.hover(
571-
position,
572571
&HoverConfig {
573572
links_in_hover: false,
574573
documentation: Some(HoverDocFormat::Markdown),
575574
},
575+
position,
576576
)
577577
.unwrap()
578578
.unwrap();
@@ -588,11 +588,11 @@ mod tests {
588588
let (analysis, position) = fixture::position(ra_fixture);
589589
let hover = analysis
590590
.hover(
591-
position,
592591
&HoverConfig {
593592
links_in_hover: true,
594593
documentation: Some(HoverDocFormat::PlainText),
595594
},
595+
position,
596596
)
597597
.unwrap()
598598
.unwrap();
@@ -608,11 +608,11 @@ mod tests {
608608
let (analysis, position) = fixture::position(ra_fixture);
609609
let hover = analysis
610610
.hover(
611-
position,
612611
&HoverConfig {
613612
links_in_hover: true,
614613
documentation: Some(HoverDocFormat::Markdown),
615614
},
615+
position,
616616
)
617617
.unwrap()
618618
.unwrap();

crates/ide/src/inlay_hints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,15 +488,15 @@ mod tests {
488488
fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) {
489489
let (analysis, file_id) = fixture::file(&ra_fixture);
490490
let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
491-
let inlay_hints = analysis.inlay_hints(file_id, &config).unwrap();
491+
let inlay_hints = analysis.inlay_hints(&config, file_id).unwrap();
492492
let actual =
493493
inlay_hints.into_iter().map(|it| (it.range, it.label.to_string())).collect::<Vec<_>>();
494494
assert_eq!(expected, actual, "\nExpected:\n{:#?}\n\nActual:\n{:#?}", expected, actual);
495495
}
496496

497497
fn check_expect(config: InlayHintsConfig, ra_fixture: &str, expect: Expect) {
498498
let (analysis, file_id) = fixture::file(&ra_fixture);
499-
let inlay_hints = analysis.inlay_hints(file_id, &config).unwrap();
499+
let inlay_hints = analysis.inlay_hints(&config, file_id).unwrap();
500500
expect.assert_debug_eq(&inlay_hints)
501501
}
502502

crates/ide/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ impl Analysis {
347347
/// Returns a list of the places in the file where type hints can be displayed.
348348
pub fn inlay_hints(
349349
&self,
350-
file_id: FileId,
351350
config: &InlayHintsConfig,
351+
file_id: FileId,
352352
) -> Cancellable<Vec<InlayHint>> {
353353
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, config))
354354
}
@@ -417,8 +417,8 @@ impl Analysis {
417417
/// Returns a short text describing element at position.
418418
pub fn hover(
419419
&self,
420-
position: FilePosition,
421420
config: &HoverConfig,
421+
position: FilePosition,
422422
) -> Cancellable<Option<RangeInfo<HoverResult>>> {
423423
self.with_db(|db| hover::hover(db, position, config))
424424
}
@@ -649,10 +649,10 @@ impl Analysis {
649649

650650
pub fn annotations(
651651
&self,
652+
config: &AnnotationConfig,
652653
file_id: FileId,
653-
config: AnnotationConfig,
654654
) -> Cancellable<Vec<Annotation>> {
655-
self.with_db(|db| annotations::annotations(db, file_id, config))
655+
self.with_db(|db| annotations::annotations(db, config, file_id))
656656
}
657657

658658
pub fn resolve_annotation(&self, annotation: Annotation) -> Cancellable<Annotation> {

crates/rust-analyzer/src/handlers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ pub(crate) fn handle_hover(
871871
) -> Result<Option<lsp_ext::Hover>> {
872872
let _p = profile::span("handle_hover");
873873
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
874-
let info = match snap.analysis.hover(position, &snap.config.hover())? {
874+
let info = match snap.analysis.hover(&snap.config.hover(), position)? {
875875
None => return Ok(None),
876876
Some(info) => info,
877877
};
@@ -1136,8 +1136,7 @@ pub(crate) fn handle_code_lens(
11361136
let lenses = snap
11371137
.analysis
11381138
.annotations(
1139-
file_id,
1140-
AnnotationConfig {
1139+
&AnnotationConfig {
11411140
binary_target: cargo_target_spec
11421141
.map(|spec| {
11431142
matches!(
@@ -1153,6 +1152,7 @@ pub(crate) fn handle_code_lens(
11531152
run: lens_config.run,
11541153
debug: lens_config.debug,
11551154
},
1155+
file_id,
11561156
)?
11571157
.into_iter()
11581158
.map(|annotation| to_proto::code_lens(&snap, annotation).unwrap())
@@ -1253,7 +1253,7 @@ pub(crate) fn handle_inlay_hints(
12531253
let line_index = snap.file_line_index(file_id)?;
12541254
Ok(snap
12551255
.analysis
1256-
.inlay_hints(file_id, &snap.config.inlay_hints())?
1256+
.inlay_hints(&snap.config.inlay_hints(), file_id)?
12571257
.into_iter()
12581258
.map(|it| to_proto::inlay_hint(&line_index, it))
12591259
.collect())

docs/dev/style.md

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -489,42 +489,6 @@ fn foo(bar: Option<Bar>) { ... }
489489
Splitting the two different control flows into two functions simplifies each path, and remove cross-dependencies between the two paths.
490490
If there's common code between `foo` and `foo_with_bar`, extract *that* into a common helper.
491491

492-
## Avoid Monomorphization
493-
494-
Avoid making a lot of code type parametric, *especially* on the boundaries between crates.
495-
496-
```rust
497-
// GOOD
498-
fn frobnicate(f: impl FnMut()) {
499-
frobnicate_impl(&mut f)
500-
}
501-
fn frobnicate_impl(f: &mut dyn FnMut()) {
502-
// lots of code
503-
}
504-
505-
// BAD
506-
fn frobnicate(f: impl FnMut()) {
507-
// lots of code
508-
}
509-
```
510-
511-
Avoid `AsRef` polymorphism, it pays back only for widely used libraries:
512-
513-
```rust
514-
// GOOD
515-
fn frobnicate(f: &Path) {
516-
}
517-
518-
// BAD
519-
fn frobnicate(f: impl AsRef<Path>) {
520-
}
521-
```
522-
523-
**Rationale:** Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
524-
This allows for exceptionally good performance, but leads to increased compile times.
525-
Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
526-
Compile time **does not** obey this rule -- all code has to be compiled.
527-
528492
## Appropriate String Types
529493

530494
When interfacing with OS APIs, use `OsString`, even if the original source of data is utf-8 encoded.
@@ -617,6 +581,42 @@ pub fn reachable_nodes(node: Node) -> FxHashSet<Node> {
617581

618582
**Rationale:** re-use allocations, accumulator style is more concise for complex cases.
619583

584+
## Avoid Monomorphization
585+
586+
Avoid making a lot of code type parametric, *especially* on the boundaries between crates.
587+
588+
```rust
589+
// GOOD
590+
fn frobnicate(f: impl FnMut()) {
591+
frobnicate_impl(&mut f)
592+
}
593+
fn frobnicate_impl(f: &mut dyn FnMut()) {
594+
// lots of code
595+
}
596+
597+
// BAD
598+
fn frobnicate(f: impl FnMut()) {
599+
// lots of code
600+
}
601+
```
602+
603+
Avoid `AsRef` polymorphism, it pays back only for widely used libraries:
604+
605+
```rust
606+
// GOOD
607+
fn frobnicate(f: &Path) {
608+
}
609+
610+
// BAD
611+
fn frobnicate(f: impl AsRef<Path>) {
612+
}
613+
```
614+
615+
**Rationale:** Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
616+
This allows for exceptionally good performance, but leads to increased compile times.
617+
Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
618+
Compile time **does not** obey this rule -- all code has to be compiled.
619+
620620
# Style
621621

622622
## Order of Imports
@@ -780,6 +780,38 @@ impl Parent {
780780
**Rationale:** easier to get the sense of the API by visually scanning the file.
781781
If function bodies are folded in the editor, the source code should read as documentation for the public API.
782782

783+
## Context Parameters
784+
785+
Some parameters are threaded unchanged through many function calls.
786+
They determine the "context" of the operation.
787+
Pass such parameters first, not last.
788+
If there are several context parameters, consider packing them into a `struct Ctx` and passing it as `&self`.
789+
790+
```rust
791+
// GOOD
792+
fn dfs(graph: &Graph, v: Vertex) -> usize {
793+
let mut visited = FxHashSet::default();
794+
return go(graph, &mut visited, v);
795+
796+
fn go(graph: &Graph, visited: &mut FxHashSet<Vertex>, v: usize) -> usize {
797+
...
798+
}
799+
}
800+
801+
// BAD
802+
fn dfs(v: Vertex, graph: &Graph) -> usize {
803+
fn go(v: usize, graph: &Graph, visited: &mut FxHashSet<Vertex>) -> usize {
804+
...
805+
}
806+
807+
let mut visited = FxHashSet::default();
808+
go(v, graph, &mut visited)
809+
}
810+
```
811+
812+
**Rationale:** consistency.
813+
Context-first works better when non-context parameter is a lambda.
814+
783815
## Variable Naming
784816

785817
Use boring and long names for local variables ([yay code completion](https://github.com/rust-analyzer/rust-analyzer/pull/4162#discussion_r417130973)).
@@ -934,7 +966,6 @@ fn dfs(graph: &Graph, v: Vertex) -> usize {
934966
let mut visited = FxHashSet::default();
935967
go(graph, &mut visited, v)
936968
}
937-
938969
```
939970

940971
**Rationale:** consistency, improved top-down readability.

0 commit comments

Comments
 (0)