Skip to content

Refactor implementation of TextControl - #19909

Merged
bors-servo merged 1 commit into
servo:masterfrom
jonleighton:refactor-textcontrol
Jan 31, 2018
Merged

bors-servo merged 1 commit into
servo:masterfrom
jonleighton:refactor-textcontrol

Conversation

@jonleighton

@jonleighton jonleighton commented Jan 30, 2018 •

Copy link
Copy Markdown
Contributor

The intention here is to make the flow more explicit. I.e. rather than
calling self.dom_select() and relying on the programmer to
know/realise that this method is provided by a trait, we call
self.selection().dom_select() and the programmer can inspect the
definition of self.selection() to follow the code.

This came out of a discussion with KiChjang here:

#19544 (comment)

Note that I tried to make "selection" be a member field of
HTML{Input,TextArea}Element but it opened up a whole can of worms with
lifetimes, so it seemed simpler to not do that since it is not
essential for this code to work.

r? @jdm and @KiChjang


This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/htmltextareaelement.rs, components/script/dom/htmlinputelement.rs, components/script/dom/textcontrol.rs
  • @fitzgen: components/script/dom/htmltextareaelement.rs, components/script/dom/htmlinputelement.rs, components/script/dom/textcontrol.rs
  • @KiChjang: components/script/dom/htmltextareaelement.rs, components/script/dom/htmlinputelement.rs, components/script/dom/textcontrol.rs

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jan 30, 2018
@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!

}
}

#[allow(unrooted_must_root)]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure this is "wrong", but I don't know the right way to do it (even after reading this) so I could do with some advice here.

@jonleighton

Copy link
Copy Markdown
Contributor Author

Personally the existing implementation doesn't bother me too much, but I do feel that this is better (assuming we can figure out the unrooted_must_root thing).

}

pub struct TextControlSelection<'a, E: TextControlElement> {
element: &'a E,

@KiChjang KiChjang Jan 30, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the problem you're facing here is this line. Since the SM GC doesn't trace the Rust stack, it does not know that there is an actively borrowed reference to the HTML{Input, TextArea}Element, and as a result it may collect the element while Rust still has a borrowed reference to it, making it "unsafe".

The solution here is to create a pointer type that SM recognizes and can trace, and there are 2 of them that we can use here: either Dom<T> or DomRoot<T>. I think DomRoot<T> may make more sense here, since all of the uses of the TextControlSelection struct does not bind to any identifier and is treated as a temporary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that replacing the type with something else is valuable. Our borrowed pointers to DOM types are fine, because they can only be obtained from properly rooted values. I'm inclined to leave the allow line as it is.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is also fine, since I don't think it's actually possible to have the GC collect the element at all in any case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried changing it to use a DomRoot<T>, which took ages to figure out and once I finally got there I still had the unrooted_must_root error... hence I reverted back to what I've submitted here and asked for advice!

@KiChjang

Copy link
Copy Markdown
Contributor

Tidy failure:

Running the WPT lint on ./tests/wpt/mozilla/tests/...
Checking files for tidiness...
./components/script/dom/textcontrol.rs:151: found an empty line following a {
./components/script/dom/textcontrol.rs:272: found an empty line following a {

@jonleighton

Copy link
Copy Markdown
Contributor Author

Tidy failure:

Thanks. I did run tidy, but I think it didn't catch this because I renamed the file (before deciding to revert to the existing naming).

The intention here is to make the flow more explicit. I.e. rather than
calling `self.dom_select()` and relying on the programmer to
know/realise that this method is provided by a trait, we call
`self.selection().dom_select()` and the programmer can inspect the
definition of `self.selection()` to follow the code.

This came out of a discussion with KiChjang here:

servo#19544 (comment)

Note that I tried to make "selection" be a member field of
HTML{Input,TextArea}Element but it opened up a whole can of worms with
lifetimes, so it seemed simpler to not do that since it is not
essential for this code to work.
@jdm

jdm commented Jan 31, 2018

Copy link
Copy Markdown
Member

@bors-servo r+
I agree that this is easier to read now. Thanks!

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 0461681 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Jan 31, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 0461681 with merge ff54a58...

bors-servo pushed a commit that referenced this pull request Jan 31, 2018
Refactor implementation of TextControl

The intention here is to make the flow more explicit. I.e. rather than
calling `self.dom_select()` and relying on the programmer to
know/realise that this method is provided by a trait, we call
`self.selection().dom_select()` and the programmer can inspect the
definition of `self.selection()` to follow the code.

This came out of a discussion with KiChjang here:

#19544 (comment)

Note that I tried to make "selection" be a member field of
HTML{Input,TextArea}Element but it opened up a whole can of worms with
lifetimes, so it seemed simpler to not do that since it is not
essential for this code to work.

r? @jdm and @KiChjang

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19909)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev
Approved by: jdm
Pushing ff54a58 to master...

@bors-servo
bors-servo merged commit 0461681 into servo:master Jan 31, 2018
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Jan 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants